Blox.Touched:Connect(function(touch) if CanTouch.Value == true then CanTouch.Value = false Torso = touch.Parent:FindFirstChild("Torso") if Torso ~= nil then local Player = Torso.Parent Blox.CanCollide = false local PlayerName = Player.Name local BloxStat = game.Players:FindFirstChild(PlayerName):FindFirstChild("leaderstats"):FindFirstChild("BloxCounter") BloxStat.Value = BloxStat.Value + 1 Ghostify() end end end)
The above code is for the collectibles in my game which I call "Blox." But for some reason, I keep getting the error: Workspace.Blox.BloxScript:30: attempt to index a nil value. I believe its coming from the end of the line which says "FindFIrstChild("BloxCounter")" but a value is named that from the start of the game so it shouldn't be nil. I really don't know what else could be going on.
local Blox = script.Parent local Pos = Blox.Position local BodyPosition = Blox.BodyPosition local BodyPositionStopped = game.Workspace.BodyPosition local PlayerValue = Blox.PlayerValue --Players = game:GetService("Players") local CanTouch = Blox.CanTouch function Ghostify() local i = 0 repeat BodyPosition.MaxForce = BodyPositionStopped.MaxForce BodyPosition.Position = Torso.Position Blox.Transparency = Blox.Transparency + 0.05 wait(0.05) i = i + 1 until i == 15 Blox:Destroy() end Blox.Touched:Connect(function(touch) if CanTouch.Value == true then CanTouch.Value = false Torso = touch.Parent:FindFirstChild("Torso") if Torso ~= nil then local Player = Torso.Parent if Player.Name ~= PlayerValue.Value then Blox.CanCollide = false local PlayerName = Player.Name local BloxStat = game.Players:FindFirstChild(PlayerName).leaderstats.BloxCounter BloxStat.Value = BloxStat.Value + 1 Ghostify() end end end end)
Line 30 is the one that sets BloxStat to BloxCounter. Also the ghostly function is there to add a smooth collecting animation so it looks like the collectible floats into your player instead of disappearing instantly.
Use a remote event instead, and have another script handle the remote event function to award it to the player in leaderstats. That way no exploiters can manipulate it and you can edit and potentially fix the script in the future.