I'm having a problem with one of my scripts. I have it coded to where every time you pick up this chicken it should give you a point every second. I have this set up and working for kills, however when I do it with the point system it gives me an error that says, "attempt to index nil with 'leaderstatstwo' ". I don't know what the problem is. This is the script inside the chicken, where the error is occuring.
while true do if script.Parent.Count.Value == true then local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent) wait(1) plr.leaderstatstwo.ChickenLB.Value = plr.leaderstatstwo.ChickenLB.Value + 1 end wait() end
This is the script that spawns the folder inside the player.
game.Players.PlayerAdded:Connect(function(player) local foldertwo = Instance.new("Folder",player) foldertwo.Name = "leaderstatstwo" local chickenlb = Instance.new("IntValue",foldertwo) chickenlb.Name = "ChickenLB" end)
Third script found inside startergui>ScreenGUI>TextLabel, this is the local script.
while wait() do local player = game.Players.LocalPlayer script.Parent.Text = "Chicken Points: "..player:WaitForChild("leaderstatstwo"):FindFirstChild("ChickenLB").Value end
It even shows the folder and the IntValue whenever you play and look under the player's folder.
The local script can't fire inside the workspace: https://developer.roblox.com/en-us/api-reference/class/LocalScript
Local scripts cannot fire when in workspace
. There is an article on the developer hub stating this.
GetPlayerFromCharacter() will return nil if the Player is not found. To stop this you must add a check to see if it has not returned nil.
if not plr then return end