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

Why doesnt the script add money to the player?

Hey, i wanted to make a script where when the player touches a block he gets money.

But it doesnt work. It says that statis is a nil value.

What am i doing wrong in this code?

script.Parent.Touched:Connect(function(hit)

local character = hit.parent
local stats = character:findFirstChild("leaderstats")	
local player = game.Players:GetPlayerFromCharacter(character)

	if player then
		local money = stats:FindFirstChild("Cash")
		local add = 50
		money.Value = money.Value + add
		print(money.Value)
	end
	
	
end)

4 comments

0

stats is a string

0

that was a mistake, edited the code how it actually is

0

":findFirstChild" is deprecated. Use ":FindFirstChild" instead. Generally the leaderstats are kept in the player. I don't know what you are doing or where you are putting your leaderstats, but I think that might be your issue.

0

You are looking for the leaderstats in the character.

1 answer

Your mistake is that you are trying to find "leaderstats" in the player's character. It is actually located in the player itself.

To find the player the character belongs to, you could use this:

--to find the leaderstats
game:GetService("Players"):FindFirstChild(character.Name).leaderstats

2 comments

0

Character's name can be changed, you might wan to do game:GetService("Players"):GetPlayerFromCharacter(character)

0

This script looks like you're treating the Character like it's an object. It's actually a property.

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