我知道你是 Stack Overflow 的新手!
我假设您问的是如何重写代码,以便只有车主可以删除汽车。
我只想指出你犯的一个错误;在 spawnCar 函数中,您已经告诉汽车移动位置,然后克隆自身并再次移动。这将导致两辆车同时出现,因此我建议您选择其中一辆。
要回答您的问题,我们可以先将 ClickDetector 添加到汽车的零件中。将此部分命名为“DeleteButton”。然后,创建一个零件,将其放在汽车的某个位置,将 ClickDetector(右键单击零件 -> 插入 -> ClickDetector)插入零件并将零件放入汽车模型中。我还希望您在模型中插入一个 StringValue(右键单击 -> 插入 -> StringValue)。
接下来,打开你的脚本,我们就开始了。
下面是一些经过编辑的代码,可帮助您为车主添加删除功能。
local model = game.Workspace.Car --or whatever the path to the model of your car is
local playerValue = game.Workspace.Car.StringValue
local deleteButton = game.Workspace.Car.DeleteButton
local button = script.Parent --or whatever the path to your button is
local destination = Vector3.new(x, y, z) --the 3D coordinates of where you'd like the car to spawn
local function spawnCar(Clicker)
model:MoveTo(destination) --use this if you want to move the car that's in the dealership
model:Clone():MoveTo(destination) --use this if you want to make a copy of the car and move that
playerValue.Value = Clicker.Name -- sets the person who clicked's name to the value.
end
local function deleteCar(Clicker)
if playerValue.Value == Click.Name then -- if the players name is the same as the owner's name
model:Destroy() -- destroy the car if it is
end
end
button.MouseClick:Connect(spawnCar) --notice it's "spawnCar", NOT "spawnCar()"
deleteButton.MouseClick:Connect(deleteCar) -- notice it's "deleteCar", NOT "deleteCar()"
总而言之,这就是我编辑的内容:我添加了两个新变量,用于播放器值的路径和一个新按钮部件;我在代码中添加了一行,将玩家值设置为玩家的名字;我创建了一个函数来检测玩家的名字是否与值相同,它将删除汽车。最后我加了一句,说当Detector被点击时,它会做delete car的功能。
您需要注意的更正:
-> MouseButton1Click/Down 用于 GUI,使用 MouseClick!
-> 选择一个或另一个(你移动了一辆车两次!)
-> 你仍然需要设置你希望汽车在目的地线产生的位置,例如local destination = Vector3.new(1, 1, 1) 等。
如果这篇文章对您有帮助,并且回答了您的问题,请务必点赞,别忘了打勾并将其标记为正确!
罗斯,我很高兴在 cmets 中提供帮助。