Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

When this script that adds cash when smoke is active is running, it crashes my game?

Ok, I don't have a weak pc or something, but this crashes my game everytime it runs:

game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player, NameOfCar)
	local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
	local location = math.random(1,7)
	local spawnlocations = game.Workspace.World.SpawnLocations
	if location == 1 then
		car:SetPrimaryPartCFrame(spawnlocations.part)
	end
	if location == 2 then
		car:SetPrimaryPartCFrame(spawnlocations.part1)
	end
	if location == 3 then
		car:SetPrimaryPartCFrame(spawnlocations.part2)
	end
	if location == 4 then
		car:SetPrimaryPartCFrame(spawnlocations.part3)
	end
	if location == 5 then
		car:SetPrimaryPartCFrame(spawnlocations.part4)
	end
	if location == 6 then
		car:SetPrimaryPartCFrame(spawnlocations.part5)
	end
	if location == 7 then
		car:SetPrimaryPartCFrame(spawnlocations.part6)
	end
	car.Parent = workspace
	car:MakeJoints()
	car.Name = player.name.."'s "..NameOfCar
	car.Wheels.RR.ChildAdded:Connect(function(child)
		if child.Name == "Smoke" then
			repeat
				if child.Rate ~= 0 then
					cash.Value = cash.Value + 10
				end
				until car == nil
		end
	end)

1 answer

✓ Accepted answer

It's because when you run "repeat until", it's adding cash infinite times per second, what do u need to fix it is making it wait:

game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player, NameOfCar)
    local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
    local location = math.random(1,7)
    local spawnlocations = game.Workspace.World.SpawnLocations
    if location == 1 then
        car:SetPrimaryPartCFrame(spawnlocations.part)
    end
    if location == 2 then
        car:SetPrimaryPartCFrame(spawnlocations.part1)
    end
    if location == 3 then
        car:SetPrimaryPartCFrame(spawnlocations.part2)
    end
    if location == 4 then
        car:SetPrimaryPartCFrame(spawnlocations.part3)
    end
    if location == 5 then
        car:SetPrimaryPartCFrame(spawnlocations.part4)
    end
    if location == 6 then
        car:SetPrimaryPartCFrame(spawnlocations.part5)
    end
    if location == 7 then
        car:SetPrimaryPartCFrame(spawnlocations.part6)
    end
    car.Parent = workspace
    car:MakeJoints()
    car.Name = player.name.."'s "..NameOfCar
    car.Wheels.RR.ChildAdded:Connect(function(child)
        if child.Name == "Smoke" then
            repeat
                wait() -- you can add how many seconds you want it to wait until it rewards the money
                if child.Rate ~= 0 then
                    cash.Value = cash.Value + 10
                end
                until car == nil
        end
    end)

1 comment

0

you can also shorten the code if you use tostring(location) for "part1" and replace the number.

This discussion is preserved as it was. New answers, comments, and votes are closed.