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

Error with the LocalPlayer, Can you help me?

I don't understand why but he sends me an error message " 16:48:55.855 - Workspace.Tycoon.Starter.Collector.Touch.Script:6: attempt to index nil with 'leaderstats' 16:48:55.855 - Stack Begin 16:48:55.856 - Script 'Workspace.Tycoon.Starter.Collector.Touch.Script', Line 6 16:48:55.856 - Stack End" and I think he can't find the localizer because when I put my roblox name it works here are my scripts for my leaderboard:

function onPlayerEntered(newPlayer)
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = ("leaderstats")
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 1000
	cash.Parent = leaderstats
	
	leaderstats.Parent = newPlayer
end

game.Players.ChildAdded:connect(onPlayerEntered)

for my script:

player = game.Players.LocalPlayer

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") 
	then
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + script.Parent.Parent.screen.Amount.Value		
		script.Parent.Parent.screen.Amount.Value = 0
	end
end)

2 answers

✓ Accepted answer

You cant use game.Players.LocalPlayer in a Server Script

There are many ways to get the player in a server script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") 
    then
local char = hit.Parent
local player = game.Players:GetPlayerFromCharacter(char)
        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + script.Parent.Parent.screen.Amount.Value        
        script.Parent.Parent.screen.Amount.Value = 0
    end
end)

2 comments

0

Thanks a lot without you I'd still be stuck

0

No problem :D It would be nice if you could accept my answer :D

A few errors here, but don't worry, I think I can fix them!

1: You must make sure that 'leaderstats' is a folder , not an IntValue. Roblox is very specific about that.

2: You define the leaderstats' parent after defining the cash. Instead, define its parent before cash because it is the parent of cash. Look to the code below to solve these first two steps:

function onPlayerEntered(newPlayer)
	local leaderstats = Instance.new("Folder", newPlayer) --this sets this as a folder and makes the parent of the leaderstats the player who just joined
	leaderstats.Name = "leaderstats"

Anymore issues please comment, if this solved your issue please accept it as an answer :)

3 comments

0

Your points are very wrong! First of all 'leaderstats' dont have to be a folder!! Second he can define the leaderstats parent after that too! I will downvote your answer

0

@itz_rennox, a little harsh but to your first point, read this article that I based my knowledge of leaderstats from: https://developer.roblox.com/en-us/articles/Leaderboards . I was only trying to help :/

1

Yes but it dont have to be a folder, its just the name that is important

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