✓ Accepted answer
The reason it's respawning one car is most likely because the .Touched event runs every time a part is touched, so if the body, wheel or "right" touch the part twice, it will spawn two cars because the game sees it as a touch, and then a touch again, to fix this, you have to add a debounce, which is a boolean which makes code run once
also there is a typo mistake
if (touched.Name == 'Body' or 'Wheel' or 'Right') then
the game would just check if the touched's name is Body. If it isn't then it will check if "Wheel" is not equal to nil (it's not because it's a string) or "Right" is not equal to nil (is not). That means that it will respawn a new car when any part touches. To fix this, just check if it's equal to Wheel or Right
if (touched.Name == 'Body' or touched.Name == 'Wheel" or touched.Name == 'Right") then -- This is correct
The full script would be:
local serverStorage = game:GetService("ServerStorage")
local car = serverStorage.Car
local respawn_point = script.Parent.Parent.Respawn_point
local debounce = true
script.Parent.Touched:Connect(function(touched)
if (touched.Name == 'Body' or touched.Name == 'Wheel' or touched.Name == 'Right') and debounce then
debounce = false
touched:Destroy()
local newCar = car:Clone()
newCar.Parent = workspace.Car
newCar.Body.Position = respawn_point.Position
wait(1)
debounce = true
end
end)
1 comment
Thx @AnasBahauddin1978