如何使此代码仅在单击玩家的汽车时删除,以使在其他人生成汽车时不会删除其他汽车?
local model = game.Workspace.Car --or whatever the path to the model of your car is
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()
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
end
button.MouseButton1Down:connect(spawnCar) --notice it's "spawnCar", NOT "spawnCar()"
答案 0 :(得分:0)
我看到您是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()"
总而言之,这就是我所编辑的内容:我添加了两个新变量,分别是玩家值和新按钮Part的路径;我在代码中添加了一行,将播放器的值设置为播放器的名称;我创建了一个函数,用于检测玩家的姓名是否与值相同,它将删除汽车。最后,我添加了一行内容,说当单击“检测器”时,它将执行删除汽车功能。
我所做的更正需要您注意:
-> MouseButton1Click / Down用于GUI,请使用MouseClick!
->选择一个或另一个(您两次移动了汽车!)
->您仍然需要设置要在目标行中生成汽车的位置,例如local destination = Vector3.new(1, 1, 1)
等。
如果此帖子对您有帮助,并且确保它回答了您的问题,请确保对这篇文章进行投票!不要忘了打勾并标记为正确!
我很乐意为您提供帮助,罗斯。