Debounce drive for cash

Hello community , i was trying to fix this thing

local UserBody = script.Parent
local User = game.Players:GetPlayerFromCharacter(UserBody)
local UserHumanoid = UserBody:WaitForChild("Humanoid")
local Sitting = false
UserHumanoid.Seated:Connect(function(IsSeated, Seat)
	if IsSeated then
		if Seat then
			Sitting = true
			local debounce = false
			while task.wait(1) do
				
				if debounce then return end
				debounce = true
				local UserHumanoidRootPart = UserBody:FindFirstChild("HumanoidRootPart")
				if Sitting == true and UserHumanoidRootPart then
					local Speed = UserHumanoidRootPart.AssemblyLinearVelocity.Magnitude
					User.leaderstats.Money.Value = User.leaderstats.Money.Value + math.floor(Speed/37.5)
					
				end
				
				debounce = false
				wait(1.5)
				
			end
		end
	else
		Sitting = false
	end
end)

i want the script give money based on speed ONLY ONE TIME EACH SECOND. it works well to me , but whenever player join , it doesnt . Any reason ? Let me know

its not local script , and parent is StarterCharScript

Why are you yielding twicee in the while do loop?

which part ? sorry , im not really good at it :face_exhaling:

What is it doing for the other players? Is it not giving anything to them, or is it giving it too often?

the problem is most likely with server having one debounce for all players ig?

it giving it too often . any reason ?? i had no clue

i think so , looks like im the only one who have the debounce but not other player

Hadn’t noticed where you have the script. Scripts in StarterCharScripts wont be replicated to the server. Moving all of your logic to a server script will make this work. I got it to work by moving it all to a characterAdded function in a server script. Each player gets in a car and drives, and the money is given. Effectively, each player would have seen their own money go up, but the server wouldn’t and the other players wouldn’t. This way, they do.

1 Like

i tried and got warn “inf yield possible on” . dont know to fix it , instead i use intvalue and it works well . “i put int value inside script” Thanks for helping me

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.