I can't give player money in my money system

So what i’m trying to make is when a player is sitting in a chair in a car he needs to get money but the problem is i can’t get it working.

script.Parent.ChildAdded:Connect(function(player)
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 10		
end)

This is my Value location where i need to add money every 1 second
image

and this is the car seat and the script inside of it is where the script is
image

2 Likes

I would recommend using a local script and sending the info over to the server to add the money.

Ye i can do it in a local script but how can i do that other part?

Always perform currency related scripting on the server for safety, along sanity checks.

That will give me 0 cash that’s not what i wanted

script.Parent.ChildAdded:Connect(function(player)
	repeat 
		player.leaderstats.Cash.Value += 10
		wait(1)
	until not Seat.Occupant	
end)

I think this is how you want it, it’ll repeated give you 10 cash till Seat.Occupant is false or nil, aka, when the player gets up from the seat

Edit: Since I never asked, did the code work originally (did it at least give you 10 cash?)

it gives me an error on the until part cause i think you didn’t added repeat i will try to make it work.

Oh right, I forgot it’s repeat until not do until, my bad. But was the code you had before giving you 10 cash since if it didn’t, it could be that that the child added wasn’t a player

Using a remote event in the local to :FireServer() and on the server [EventLocation].OnServerEvent:Connect(function(player)

In there you can add player.leaderstats.Cash.Value += 10

So it would look something like on the Server:

[DefineEventLocation].OnServerEvent:Connect(function(player)
        -- You can add some if statements to make sure the player is not exploiting
        player.leaderstats.Cash.Value += 10
end)

i didn’t give me money my previous code idk why that’s why i asked on the forum

When i did try to fix it with your script it doesn’t work i get the error:

leaderstats is not a valid member of BodyGyro “Workspace.TurteleDJCar.DriveSeat.BodyGyro”

The error is on line 5

Then I believe what you should do is check whenever a palyer sits down, you can do this via Seat.Occupant, which has a value whenever someone sits down, which is their humanoid. It’s set to nil if no one is sitting or has gotten up. You can try

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local human = seat.Occupant
	if human then
		local plr = game.Players:GetPlayerFromCharacter(human.Parent)
		if not plr then return end
		plr.leaderstats.Cash.Value += 10
	end
end)

And also, you got taht error because a child was added to the seat that wasn’t a player, it’s better to check the Occupant instead

Edit: Added a check so it wont try to give money to something that isn’t the character of a player

hey it did work ty for all your help have a nice day!

Anytime! Make sure to set my answer as the solution then since it helped you! If you have any more issues don’t be afraid to make another post!

Also how can i make this with an repeat until so they get money every 1 second but if they are out the car it does stop?

Oh right, I forgot to add that functionality, use this

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local human = seat.Occupant
	if human then
		local plr = game.Players:GetPlayerFromCharacter(human.Parent)
		if not plr then return end
		repeat
			plr.leaderstats.Cash.Value += 10
			wait(1)
		until not Seat.Occupant
	end
end)

Ty for helping me if you didn’t help i would sit here the whole day searching!

Glad it worked out in the end! Again, make sure to set my reply as the solution for anyone that may see this who’s having a similar issue! Once again, if you have anymore issues, don’t hesitate to make another post!