#Help with finding the cause of the error in my script

1 messages · Page 1 of 1 (latest)

real thistle
#

everytime I run this code I keep receiving multiple errors stating: 11:35:15.319 Workspace.InstanceDDelete:42: attempt to index nil with 'leaderstats' - Server

game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local value = Instance.new("IntValue")
value.Name = "Gold"
value.Parent = folder
value.Value = 0
end)

local mg = workspace:WaitForChild("Maingame")
local Coins = mg:WaitForChild("Coins")

local Debris = game:GetService("Debris")

local ball = Instance.new("Part")
ball.Anchored = false
ball.Shape = Enum.PartType.Ball
ball.TopSurface = Enum.SurfaceType.Smooth
ball.BottomSurface = Enum.SurfaceType.Smooth
ball.Size = Vector3.new(1, 1, 1)

local RNG = Random.new()
local MAX_VELOCITY = 10

while true do
local newBall = ball:Clone()
newBall.BrickColor = BrickColor.random()
newBall.CFrame = CFrame.new(0, 30, 0)
newBall.Velocity =
Vector3.new(RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY), 0, RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY))
newBall.Parent = game.Workspace
Debris:AddItem(newBall, 2)

task.wait(0.01)
        
    newBall.Touched:Connect(function(otherpart)
        local humanoid = otherpart.Parent:FindFirstChild("Humanoid")        
        local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)                
        local rm = math.random(1, 10)
        player.leaderstats.Gold.Value += rm        
end)    

end

dusty nebula
#

check if it's a player or not

nimble sinew
#

"Attempt to index nil with leaderstats"

#

What does that tell you?

#

Leaderstats either doesn't exist or cannot be indexed, therefore cannot be deleted.

#

Try doing lua if not player:IsA("Player") then return end
to check if you're actually getting a player!

real thistle
#

it still wont work

vapid remnantBOT
#

studio** You are now Level 3! **studio

crisp cradle
# real thistle it still wont work

you're missing an if statement. this is returning nil local player = game.Players:GetPlayerFromCharacter(otherpart.Parent). just check if this is nil in an if statement. how is it nil? maybe the ball touched the baseplate or something like that, which is definitely not a player. simple Thumbs

real thistle
#

ohh I unddedrstand now

#

the balls that was being generated was touching a part the I coded only to check for a humanoid