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

Why does my local variable have a error?(local Money)

I'm not sure why this local variable has an error on it i assigned it a value so whats the problem?

script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter
local Money = player.leaderstats.Money -- my question,I just want to know whats causing the "local" error it says expected '(' and <string> what does it mean

if hit.Parent == game.Workspace.PartStorage then
	wait(0.4)
	Money.Value = Money.Value + hit.Value
	hit:Destroy()
	
	
end	
	
	end 
	
	
	
	
end)

2 answers

✓ Accepted answer

You are not using GetPlayerFromCharacter correctly. It is a function and you need to call it passing it the instance to attempt to find the player. If no player is found then nil is returned.

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- hit.Parent would be the model
	if player then -- check that player is not nil
		-- code
	end
end)

I hope this helps. Please comment if you have any other questions about this code.

This should work. You aren't getting the player correctly and for that reason you can't get the leaderstats.

local user = game.Players:GetPlayerFromCharacter(hit.Parent)
		local stats = user:findFirstChild("leaderstats")

			if stats ~= nil then 
			local cash = stats:findFirstChild("Money")
			cash.Value = cash.Value + numbervalue

1 comment

0

thanks this helps me understand im not getting the player correctly

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