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

Why can't I take cash from a player using a Fireserver?

I'm trying to make my game take 250 from players when I activate a Fireserver.

game.ReplicatedStorage.Take250.OnServerEvent:Connect(function(player)
	game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value - 250
	
end)

However I get this error when i activate the Fireserver

ServerScriptService.Take cash:2: attempt to index nil with 'leaderstats'

What does this mean and how can I fix it.

Also this is just a part of my leaderboard script in case it helps

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	-- Display an 'IntValue' on leaderboard
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats

2 comments

0

Everything involving in-game currency should be server-sided, so why are you firing an event from the client?

0

i'm making a car purchase gui and everything works but taking the cash

1 answer

✓ Accepted answer

the issue is with the server side code, you did game.Players.LocalPlayer.leaderstats.Cash.Value - 250, but the thing is you can't do game.Players.LocalPlayer in a server script.. because remember, LocalScripts run on the player's computer, therefore, doing game.Players.LocalPlayer in a LocalScript returns the player object whose computer is running the localscript;

so do it like this:

game.ReplicatedStorage.Take250.OnServerEvent:Connect(function(player)
    player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 250
end)

also make sure the code that initializes the leaderstats is in a server script

1 comment

0

Your a legend!

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