01 | Blox.Touched:Connect( function (touch) |
02 | if CanTouch.Value = = true then |
03 | CanTouch.Value = false |
04 | Torso = touch.Parent:FindFirstChild( "Torso" ) |
05 | if Torso ~ = nil then |
06 | local Player = Torso.Parent |
07 | Blox.CanCollide = false |
08 | local PlayerName = Player.Name |
09 | local BloxStat = game.Players:FindFirstChild(PlayerName):FindFirstChild( "leaderstats" ):FindFirstChild( "BloxCounter" ) |
10 | BloxStat.Value = BloxStat.Value + 1 |
11 | Ghostify() |
12 | end |
13 | end |
14 | 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.
01 | local Blox = script.Parent |
02 | local Pos = Blox.Position |
03 | local BodyPosition = Blox.BodyPosition |
04 | local BodyPositionStopped = game.Workspace.BodyPosition |
05 | local PlayerValue = Blox.PlayerValue |
06 | --Players = game:GetService("Players") |
07 | local CanTouch = Blox.CanTouch |
08 |
09 | function Ghostify() |
10 | local i = 0 |
11 | repeat |
12 | BodyPosition.MaxForce = BodyPositionStopped.MaxForce |
13 | BodyPosition.Position = Torso.Position |
14 | Blox.Transparency = Blox.Transparency + 0.05 |
15 | wait( 0.05 ) |
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.