Touch to get cash not working!

Ok I have my script but it is not working. I am trying to give the user 100 cash when they touch the brick and then wait an hour to receive 100 more cash.
Here is my leaderboard script.
game.Players.PlayerAdded:Connect(function(plr)
local leader = Instance.new(“Folder”, plr)
leader.name = ‘leaderstats’
local cash = Instance.new(“IntValue”, leader)
cash.Name = ‘Cash’

end)

Here is my touch script for money:
db = false

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if db == false then
db = true
player.leaderstats.cash.Value = player.leader.cash.Value + 100

		wait(3600)
		player.leaderstats.cash.Value = player.leader.cash.Value + 100
		
		db = false
	end
end

end)
It is not working, anyone know the error?
Thanks in advance!

Hi so on the addition part of your script you didn’t finish the word “leaderstats”

	wait(3600)
		player.leaderstats.cash.Value = player.leaderstats.cash.Value + 100
		
		db = false
	end
end
2 Likes

Where do I put that variable? Sorry I am still new to Lua a bit.

1 Like

he has that just not in the code block, and remove the .Name cause that would be trying to identify the model object from the stringname, if you were to do it like then it would just be the

game.Players:FindFirstChild(hit.Parent.Name)

My leaderstats folder is named leader though.

db = false

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:FindFirstChild(hit.Parent.Name)
if db == false then
db = true
player.leader.cash.Value = player.leader.cash.Value + 100

		wait(3600)
		player.leader.cash.Value = player.leadercash.Value + 100
		
		db = false
	end
end

end)
This is my new code btw.

db = false

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:FindFirstChild(hit.Parent.Name)
if db == false then
db = true
player.leader.cash.Value = player.leader.cash.Value + 100

		wait(3600)
		player.leader.cash.Value = player.leadercash.Value + 100
		
		db = false
	end
end

end)
Still not working though.

okay so basically you are trying to use a hierarchy type system, The guy before made a mistake and removed it so sorry for that confusion you had it almost right the first time you only just had one word mistyped and that was all

game.Players.PlayerAdded:Connect(function(plr)
local leader = Instance.new(“Folder”, plr)
leader.name = ‘leaderstats’
local cash = Instance.new(“IntValue”, leader)
cash.Name = ‘Cash’

end)

so Above here you are creating something called “leaderstats” under the player

db = false

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if db == false then
db = true

player.leaderstats.cash.Value = player.leaderstats.cash.Value + 100
		wait(3600)
		
		
		db = false
	end
end

so basically you only had the one original error in the script where you typed player.leader.cash when its player.leaderstats.cash

Hope this makes a bit more sense!

Do you need the script to have like a cooldown?

Yes, 1 hour cooldown. That is what I want

Ok try this script then.

db = false
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if db == false then
			db = true
			script.Parent.Transparency = 1
			script.Parent.CanCollide = false
			player.leaderstats.cash.Value = player.leaderstats.cash.Value + 50
			wait (3600)
			script.Parent.Transparency = 0
			script.Parent.CanCollide = true
			db = false
		end
	end
end)	

I tested it and it should work.

You have to do:
player.leaderstats.cash.Value = player.leaderstats.cash.Value + 100
What you’re doing is:
player.leaderstats.cash.Value = player.leader.cash.Value + 100

Or you can do:
player.leader.cash.Value = player.leader.cash.Value + 100

Still not working, where do I put my leadeboard script?

Can you check the Output? It’s in the view tab if you don’t know

Let me send a screenshot of my output.

The examples provided above should work fine. Leaderboard goes into ServerScriptService and the actual “Touch script” goes into your part in workspace.

Test it out right now, let me send screenshot of output.
It says there is an error in Line 9 which is the adding of the cash.

It says something about line 9 which adds the cash to the players leaderboard.

Should be:

player.leaderstats.cash.Value = player.leaderstats.cash.Value + 100