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

attempt to index local 'player' (a nil value)?

after someone helped me with my other script in my simulator game, i tried making a "Delete Your Data" button. But now on line 4, its saying attempt to index local 'player' (a nil value) can someone help me?

local delete = game:GetService("ServerStorage")

script.Parent.MouseButton1Click:Connect(function(player)
	delete:FindFirstChild(player.UserId).Parent = workspace
	wait(1)
	delete:FindFirstChild(player.UserId).Parent = delete
	script.Parent.Parent.TextLabel.Text = "Deleted"
	wait(1.5)
	script.Parent.Parent.Visible = false
	script.Parent.Parent.TextLabel.Text = "Are You Sure? This Will Delete All Your Data."
end)

my stats script:

local serverStorage = game:GetService("ServerStorage")
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")

game:GetService("Players").PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local strength = Instance.new("NumberValue")
    strength.Name = "Strength"
    strength.Value = 0
    strength.Parent = leaderstats

    local rebirths = Instance.new("IntValue")
    rebirths.Name = "Rebirths"
    rebirths.Value = 0
    rebirths.Parent = leaderstats

    local dataFolder = Instance.new("Folder")
    dataFolder.Name = player.Name
    dataFolder.Parent = serverStorage:WaitForChild("RemoteData")

    local debounce = Instance.new("BoolValue")
    debounce.Name = "Debounce"
    debounce.Parent = dataFolder

    local success, errormessage = pcall(function()
        datastore:GetAsync(player.UserId)
    end)

    if success then
        local LoadArray = datastore:GetAsync(player.UserId)
        if LoadArray then
            strength.Value = LoadArray[strength.Name]
            rebirths.Value = LoadArray[rebirths.Name]
        end
    else
        warn(errormessage)
    end

    player.CharacterAppearanceLoaded:Connect(function(character)
        local humid = character:WaitForChild("Humanoid")
        local bds = humid:WaitForChild("BodyDepthScale")
        local bhs = humid:WaitForChild("BodyHeightScale")
        local bws = humid:WaitForChild("BodyWidthScale")
        local hhs = humid:WaitForChild("HeadScale")
        local pbw = player:WaitForChild("Backpack"):WaitForChild("Weight")

        local default1 = 0.5
        local default2 = 16
        local base = 250

        local p1 = default1 + (strength.Value / base)
        local s1 = default2 + (strength.Value / base)

        bds.Value = p1
        bhs.Value = p1
        bws.Value = p1
        hhs.Value = p1
        humid.WalkSpeed = s1

        strength:GetPropertyChangedSignal("Value"):Connect(function()
            local p2 = default1 + (strength.Value / base)
            local s2 = default2 + (strength.Value / base)

            bds.Value = p2
            bhs.Value = p2
            bws.Value = p2
            hhs.Value = p2
            pbw.Handle.Size = Vector3.new(p2, p2, p2)
            humid.WalkSpeed = s2
        end)
    end)
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
    local lead = player:WaitForChild("leaderstats")
    local str = lead:WaitForChild("Strength")
    local reb = lead:WaitForChild("Rebirths")

    local SaveArray = {}
    SaveArray[str.Name] = str.Value
    SaveArray[reb.Name] = reb.Value

    local success, errormessage = pcall(function()
        datastore:SetAsync(player.UserId, SaveArray)
    end)

    if not success then
        warn(errormessage)
    end
end)

1 answer

MouseButton1Click does not have a player variable. Instead, use the localplayer:

local delete = game:GetService("ServerStorage")
player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()	
	--first lets check if the player exists: 
	print("Player", player)

	--Now let's check if we're getting the player's userId exists
	print("UserId", player.UserId)

	--Lastly, we're checking if the player.UserId exists in the serverstorage
	print(delete:FindFirstChild(player.UserId))

    delete:FindFirstChild(player.UserId).Parent = workspace
    wait(1)
    delete:FindFirstChild(player.UserId).Parent = delete
    script.Parent.Parent.TextLabel.Text = "Deleted"
    wait(1.5)
    script.Parent.Parent.Visible = false
    script.Parent.Parent.TextLabel.Text = "Are You Sure? This Will Delete All Your Data?"
end)

12 comments

0

line 5 attempt to index a nil value

0

print(delete:FindFirstChild(player.UserId)) on line 5 and print(player.UserId) on line four to see to see if either are a nil value

0

i put delete:FindFirstChild(player.UserId) on line 5 and now its says same but on line 7

0

i tried putting parent the parent into workspace now its says same thing line 5

0

when you added those two prints what did it print? I might be a bit confusing so I'll edit my post to show you what I mean

0

edited. tell me what those values print when you press run. it'll still error, but this will help us know what's going wrong.

0

Player AlbertoMiAmigo2 UserId 268163689 nil i think its because you cant acces things from server storage want me to post the data store script?

0

Can you pls send me a screenshot of "268163689" in the serverstorage just so we can make sure it's there? also, maybe try delete:FindFirstChild(tostring(player.UserId)) instead

0

268163689 is my userid but okay are you talking about the data store? i will send both

0

https://prnt.sc/pbiym5 what it prints in output

0

the part i think your talking about https://prnt.sc/pbiyvi

0

hi did you ever get this working?

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