0

# Script creates <1 model when touched with a part?(MACHINE LEARNING/ AI)

I am working on an ai algorithm witch learns to drive cars. So basically the script i have made for respawning the car it seems to be respawning more than 1 car. As in like i want to respawn the car with a part touch script; script.Parent.Touched:Connect(function(touched) and that just spawns the new car in.

My script:

local car = game.ServerStorage.Car
local respawn_point = script.Parent.Parent.Respawn_point

script.Parent.Touched:Connect(function(touched)
if touched.Name == 'Body' or 'Wheel' or 'Right' then
touched:Destroy()
local new_car = car:Clone()
new_car.Parent = game.Workspace.Car
new_car.Body.Position = respawn_point.Position
end

end)


1
Edited 5 days ago

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 -- This is not how programming works


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 -- We set debounce to false so that the above if statement is always false and this code will run once
touched:Destroy()
local newCar = car:Clone()
newCar.Parent = workspace.Car
newCar.Body.Position = respawn_point.Position
wait(1) -- Set the debounce to true after a few seconds
debounce = true -- how the touched event can run again
end
end)

0
Thx @AnasBahauddin1978 Retallack445 75 — 4d