Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to fix "attempt index nil with 'LoadCharacter' " ?

Asked by 3 years ago
Edited 3 years ago

I was tried to make UI play button for my menu. I added script (Local script) , but there is error in my script about LoadCharacter() - Players.Egor_Naum.PlayerGui.ChooseTeamGui.Play.PlayTextButton.ClickScript:6: attempt to index nil with 'LoadCharacter' . I tested ALMOST all stuff. How to fix?

script.Parent.MouseButton1Click:Connect(function(plr)
    if  game.Players.LocalPlayer.TeamColor ~= BrickColor.new("Medium stone grey") then -- check if player choosed team (if (s)he not choosing team)
        game.Lighting.Blur.Size = 0 -- Makes blut size to 0
        plr:LoadCharacter(plr) --Refresh character
        script.Parent.Parent.Parent.Enabled = false -- Makes GUI not enabled
        script.Parent.Parent.Parent.MenuMusic:Stop() -- Stops music
game.Workspace.Camera.CameraType = Enum.CameraType.Custom -- Makes default (custom) camera

        print(game.StarterGui.ChooseTeamGui.ChooseTeam.Frame.Visible )
        --If player dont choosed team (if (s)he choosign team --
    else
        script.Parent.Parent.ChooseTeamWarning.TextTransparency = 0-- Makes warning text visible
        wait(1)
        script.Parent.Parent.ChooseTeamWarning.TextTransparency = 1-- Makes warning text invisible
    end
end)

1 answer

Log in to vote
0
Answered by 3 years ago

You are attempting to get the player from your function .MouseButton1Click(), however .MouseButton1Click does not create an output. Alternatively, you can use game.Players.LocalPlayer as you are using a LocalScript. Here is the API Reference for MouseButton1Click: https://developer.roblox.com/en-us/api-reference/event/GuiButton/MouseButton1Click

local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
    if  game.Players.LocalPlayer.TeamColor ~= BrickColor.new("Medium stone grey") then -- check if player choosed team (if (s)he not choosing team)
        game.Lighting.Blur.Size = 0 -- Makes blut size to 0
        plr:LoadCharacter(plr) --Refresh character
        script.Parent.Parent.Parent.Enabled = false -- Makes GUI not enabled
        script.Parent.Parent.Parent.MenuMusic:Stop() -- Stops music
game.Workspace.Camera.CameraType = Enum.CameraType.Custom -- Makes default (custom) camera

        print(game.StarterGui.ChooseTeamGui.ChooseTeam.Frame.Visible )
        --If player dont choosed team (if (s)he choosign team --
    else
        script.Parent.Parent.ChooseTeamWarning.TextTransparency = 0-- Makes warning text visible
        wait(1)
        script.Parent.Parent.ChooseTeamWarning.TextTransparency = 1-- Makes warning text invisible
    end
end)

Hope this helps!

0
It says " LoadCharacter can only be called by the backend server " Egor_Naum 5 — 3y
0
Thanks! It worked. Egor_Naum 5 — 3y
Ad

Answer this question