我正在开发一款 Roblox 游戏,我需要克隆一个对象并允许玩家在抛出克隆对象后拾取该对象。但是,我遇到了无法拾取克隆对象的问题。
这是我的本地投掷脚本
local userInputService = game:GetService("UserInputService")
local playersService = game:GetService("Players")
local tool = script.Parent.Parent
local humanoid = playersService.LocalPlayer.Character:WaitForChild("Humanoid")
local workspace = game:GetService("Workspace")
local isEquipped = false
local mouse = playersService.LocalPlayer:GetMouse()
local function createToolCopy()
local toolCopy = tool:Clone()
toolCopy.Parent = workspace
local throwDirection = (mouse.Hit.p - toolCopy.Handle.Position).unit
local throwForce = 100
toolCopy.Handle.Velocity = throwDirection * throwForce
isEquipped = false
tool:Destroy()
end
tool.Equipped:Connect( function() isEquipped = true end)
tool.Unequipped:Connect(function() isEquipped = false end)
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if isEquipped and not gameProcessedEvent then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
createToolCopy()
end
end
end)
这是我的用于拿起杯子的全局脚本
local clickDetector = script.Parent
local tool = clickDetector.Parent
if clickDetector then
clickDetector.MaxActivationDistance = 10
clickDetector.MouseClick:Connect(function(playerWhoClicked: Player)
print("Player clicked the clickDetector")
tool.Parent = playerWhoClicked.Backpack
end)
else
warn("clickDetector not found in the object.")
end
我也提供了一个大概的了解,这是cup的结构:
├─ clickDetector/
│ ├─ GlobalScriptForPickUp
│ ├─ LocalScriptForThrowing
├─ ModelOfCup
├─ Handle/
│ ├─ TouchInterest
│ ├─ WeldConstraint
我希望它是这样的: 玩家拿起杯子 -> 拿起它 -> 鼠标右键 -> 库存中的杯子被克隆到工作区 -> 飞行 -> 从库存中消失 -> 玩家跑到克隆的杯子并可以投掷再次(尽管将来我想根据压力机的长度制作一个系统投掷力, 如果力大于一定值,杯子就会破裂)
发生了什么: 除了最后一步之外,上述所有步骤都已完成,这就是我写这篇文章的原因
这是一个典型的脚本,可以在禁用过滤的情况下工作。 尽管仅引入“启用过滤”后,服务器会过滤客户端可以执行的操作。因此,客户端现在不可能再创建服务器脚本(在您的情况下为全局脚本),因为它们也能够影响其他客户端。
由于本地脚本和客户端创建了一个包含服务器脚本的新工具,因此服务器不会检测到该服务器脚本是由客户端创建的,因为它不会复制到服务器,也不会复制到工具。
考虑使用 RemoteEvents 在客户端和服务器之间进行通信。
我建议你将客户端上的工具激活检测与服务器上的其余部分分开(例如抛出工具和克隆它)