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

How to change brick color?

PLEASE NOTE The title is supposed to be "Why is my script not working at all?"

I have this cash script that is in a part and the part is in workspace, The script basically is supposed to add 100 to the value on the "Cash" leaderstat. I am having trouble figuring out whats wrong with it and why it is not working...I have tried testing it on Studio and I also tested it on a roblox world but it is not working.. the output is empty and I can't seem to find the problem:

debounce = true

function Touch(part)
	if debounce then
		debounce = false
		local h = part.Parent:FindFirstChild("Humaniod")
		if h ~= nil then
			local player = game.Players.GetPlayerFromCharacter(part.Parent)
			if player then
				player:WaitForChild("leaderstats")
				player.leaderstats:WaitForChild("Cash")
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100
			end
		end
		debounce = true
	end
end

script.Parent.Touched:connect(Touch)

If somebody can help me that would be awesome... :D thanks

2 answers

✓ Accepted answer

In cases like these you should either use print() statements or breakpoints to find what is and is not working.

The first thing I noticed was that you used "connect" on the event line. connect is actually deprecated now, so we should use Connect.

Continuing on, we can get rid of the logic to check for a humanoid. All we need is the GetPlayerFromCharacter() call and to check for what it returns (the player) as you're already doing.

One of the possibilities I can think of is that leaderstats or Cash in leaderstats may not exist. Again, try to use print() statements to find what's executing.

Hope I was of some help.

1 comment

0

Thank you for the help I found out the problem in the end was: I needed to do :GetPlayerFromCharacter and I did .GetPlayerFromCharacter

This should work. :)

debounce = true

function onTouch(Part)
	for _, player in ipairs(game.Players:GetPlayers()) do
		if player:FindFirstChildren("leaderstats") then
			player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100
			debounce = false
		end
	end
end

script.Parent.Touched:connect(onTouch)

1 comment

0

Remember to hit that Accept Answer Button :-)

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