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

LocalPlayer works in Studio, however not in-game?

Asked by
traigla 75
8 years ago

I have this script here that works in Studio in a regular script. When I tried it in-game it had an error on line 7 saying LocalPlayer was nil?

Any help is appreciated.

(I also tried this in a localscript but nothing happened.

children = script.Parent:GetChildren()
local mainParts = script.Parent.Parent.Parent.MainParts

for _,child in pairs(children) do
    if child:IsA("TextButton") then
        child.MouseButton1Click:connect(function()
            if game:GetService('BadgeService'):UserHasBadge(game.Players.LocalPlayer.userId, 255119622) or game:GetService('BadgeService'):UserHasBadge(game.Players.LocalPlayer.userId, 255119711) or game.Players.LocalPlayer:GetRankInGroup(942208) == 230 then
                if game.Players.LocalPlayer.Booked.Value == false then
                    if child.Booked.Value == false then
                        script.Parent.Status.Text = "Seat "..child.Name.." has been booked by "..game.Players.LocalPlayer.Name.."."
                        script.Parent.Seats.Value = script.Parent.Seats.Value - 1
                        if script.Parent.Seats.Value == 1 then
                            script.Parent.SeatsRemain.Text = script.Parent.Seats.Value.." seat is remaining."       
                        else
                            script.Parent.SeatsRemain.Text = script.Parent.Seats.Value.." seats are remaining."
                        end
                        local newgui = script.Parent.Confirm_Booked:clone()
                        newgui.Main.Number.Text = child.Name
                        newgui.Parent = game.Players.LocalPlayer.PlayerGui
                        child.Text = ""
                        child.BackgroundColor3 = Color3.new(255,0,0)
                        child.Booked.Value = true
                        child.AutoButtonColor = false
                        game.Players.LocalPlayer.Booked.Value = true
                        child.Username.Value = game.Players.LocalPlayer.Name
                        if child.Name == "1C" or "2C" or "3C" or "4C" or "5C" or "6C" or "7C" or "8C" or "9C" or "10C" or "11C" or "12C" or "13C" or "14C" or "15C" or "16C" or "1B" or "2B" or "3B" or "4B" or "5B" or "6B" or "7B" or "8B" or "9B" or "10B" or "11B" or "12B" or "13B" or "14B" or "15B" or "16B" then
                            mainParts.BC.SurfaceGui[child.Name].Text = child.Name.." - "..game.Players.LocalPlayer.Name
                            mainParts.BC.SurfaceGui[child.Name].TextColor3 = Color3.new(255,0,0)
                            mainParts.BC.SurfaceGui[child.Name].Player.Value = game.Players.LocalPlayer.Name
                        end
                        if child.Name == "1A" or "2A" or "3A" or "4A" or "5A" or "6A" or "7A" or "8A" or "9A" or "10A" or "11A" or "12A" or "13A" or "14A" or "15A" or "16A" then
                            mainParts.A.SurfaceGui[child.Name].Text = child.Name.." - "..game.Players.LocalPlayer.Name
                            mainParts.A.SurfaceGui[child.Name].TextColor3 = Color3.new(255,0,0)
                            mainParts.BC.SurfaceGui[child.Name].Player.Value = game.Players.LocalPlayer.Name
                        end
                    end
                elseif child.Username.Value ~= game.Players.LocalPlayer.Name then
                    if not (game.Players.LocalPlayer.PlayerGui:findFirstChild("Error_PlayerBooked")) then
                        local newgui = script.Parent.Error_PlayerBooked:clone()
                        newgui.Parent = game.Players.LocalPlayer.PlayerGui
                        newgui.Script.Disabled = false
                    end
                elseif child.Username.Value == game.Players.LocalPlayer.Name then
                    script.Parent.Seats.Value = script.Parent.Seats.Value + 1
                    if script.Parent.Seats.Value == 1 then
                        script.Parent.SeatsRemain.Text = script.Parent.Seats.Value.." seat is remaining."       
                    else
                        script.Parent.SeatsRemain.Text = script.Parent.Seats.Value.." seats are remaining."
                    end
                    if game.Players.LocalPlayer.PlayerGui:findFirstChild("Confirm_Booked") then
                        game.Players.LocalPlayer.PlayerGui.Confirm_Booked:remove()
                    end
                    child.Text = "Book"
                    child.BackgroundColor3 = Color3.new(0,255,0)
                    child.Booked.Value = false
                    child.AutoButtonColor = true
                    game.Players.LocalPlayer.Booked.Value = false
                    child.Username.Value = ""
                    if child.Name == "1C" or "2C" or "3C" or "4C" or "5C" or "6C" or "7C" or "8C" or "9C" or "10C" or "11C" or "12C" or "13C" or "14C" or "15C" or "16C" or "1B" or "2B" or "3B" or "4B" or "5B" or "6B" or "7B" or "8B" or "9B" or "10B" or "11B" or "12B" or "13B" or "14B" or "15B" or "16B" then
                        mainParts.BC.SurfaceGui[child.Name].Text = child.Name.." - Available"
                        mainParts.BC.SurfaceGui[child.Name].TextColor3 = Color3.new(0,255,0)
                    end
                    if child.Name == "1A" or "2A" or "3A" or "4A" or "5A" or "6A" or "7A" or "8A" or "9A" or "10A" or "11A" or "12A" or "13A" or "14A" or "15A" or "16A" then
                        mainParts.A.SurfaceGui[child.Name].Text = child.Name.." - Available"
                        mainParts.A.SurfaceGui[child.Name].TextColor3 = Color3.new(0,255,0)
                    end
                end
            else
                local newgui = script.Parent.Error_NoShirt:clone()
                newgui.Parent = game.Players.LocalPlayer.PlayerGui
                newgui.Script.Disabled = false
            end
        end)
    end
end

game.Players.PlayerAdded:connect(function(plr)
    local bookedvalue = script.Parent.Booked
    local newvalue = bookedvalue:clone()
    newvalue.Parent = plr
end)

0
Localplayer can only be used in local scripts, in serverscripts it wont work, in solo it does work, but in real servers it wont work, it has to be in a local script Scriptree 125 — 8y
0
I tried it in a localscript and nothing at all happens. traigla 75 — 8y

1 answer

Log in to vote
0
Answered by
Marios2 360 Moderation Voter
8 years ago

The solution is very simple here.

Scripts run immediately when the server is created - and not necessarily when the first Player object is created. As there is no Player I nstance yet, the game refers to a nil value!

Supposing your script is in a LocalScript in Backpack, PlayerGui or PlayerScripts, all you need to do is add this line at the top:

repeat wait() until game.Players.LocalPlayer

You cannot have LocalPlayer in an ordinary script, it's always nil for them.

Along with that, you should also add FindFirstChild to booked.

Here's the full script:

 --This script is flooded with deprecation
repeat wait() until game.Players.LocalPlayer
children = script.Parent:GetChildren()
local mainParts = script.Parent.Parent.Parent.MainParts

for _,child in pairs(children) do
    if child:IsA("TextButton") then
        child.MouseButton1Click:connect(function()
            if game:GetService('BadgeService'):UserHasBadge(game.Players.LocalPlayer.userId, 255119622) or game:GetService('BadgeService'):UserHasBadge(game.Players.LocalPlayer.userId, 255119711) or game.Players.LocalPlayer:GetRankInGroup(942208) == 230 then
                if game.Players.LocalPlayer:FindFirstChild("Booked").Value == false then --Just so we're safe
                    if child.Booked.Value == false then
                        script.Parent.Status.Text = "Seat "..child.Name.." has been booked by "..game.Players.LocalPlayer.Name.."."
                        script.Parent.Seats.Value = script.Parent.Seats.Value - 1
                        if script.Parent.Seats.Value == 1 then
                            script.Parent.SeatsRemain.Text = script.Parent.Seats.Value.." seat is remaining."       
                        else
                            script.Parent.SeatsRemain.Text = script.Parent.Seats.Value.." seats are remaining."
                        end
                        local newgui = script.Parent.Confirm_Booked:Clone()
                        newgui.Main.Number.Text = child.Name
                        newgui.Parent = game.Players.LocalPlayer.PlayerGui
                        child.Text = ""
                        child.BackgroundColor3 = Color3.new(255,0,0)
                        child.Booked.Value = true
                        child.AutoButtonColor = false
                        game.Players.LocalPlayer.Booked.Value = true
                        child.Username.Value = game.Players.LocalPlayer.Name
                        if child.Name == "1C" or "2C" or "3C" or "4C" or "5C" or "6C" or "7C" or "8C" or "9C" or "10C" or "11C" or "12C" or "13C" or "14C" or "15C" or "16C" or "1B" or "2B" or "3B" or "4B" or "5B" or "6B" or "7B" or "8B" or "9B" or "10B" or "11B" or "12B" or "13B" or "14B" or "15B" or "16B" then
                            mainParts.BC.SurfaceGui[child.Name].Text = child.Name.." - "..game.Players.LocalPlayer.Name
                            mainParts.BC.SurfaceGui[child.Name].TextColor3 = Color3.new(255,0,0)
                            mainParts.BC.SurfaceGui[child.Name].Player.Value = game.Players.LocalPlayer.Name
                        end
                        if child.Name == "1A" or "2A" or "3A" or "4A" or "5A" or "6A" or "7A" or "8A" or "9A" or "10A" or "11A" or "12A" or "13A" or "14A" or "15A" or "16A" then
                            mainParts.A.SurfaceGui[child.Name].Text = child.Name.." - "..game.Players.LocalPlayer.Name
                            mainParts.A.SurfaceGui[child.Name].TextColor3 = Color3.new(255,0,0)
                            mainParts.BC.SurfaceGui[child.Name].Player.Value = game.Players.LocalPlayer.Name
                        end
                    end
                elseif child.Username.Value ~= game.Players.LocalPlayer.Name then
                    if not (game.Players.LocalPlayer.PlayerGui:FindFirstChild("Error_PlayerBooked")) then --findFirstChild() is deprecated
                        local newgui = script.Parent.Error_PlayerBooked:Clone() --clone() is also deprecated
                        newgui.Parent = game.Players.LocalPlayer.PlayerGui
                        newgui.Script.Disabled = false
                    end
                elseif child.Username.Value == game.Players.LocalPlayer.Name then
                    script.Parent.Seats.Value = script.Parent.Seats.Value + 1
                    if script.Parent.Seats.Value == 1 then
                        script.Parent.SeatsRemain.Text = script.Parent.Seats.Value.." seat is remaining."       
                    else
                        script.Parent.SeatsRemain.Text = script.Parent.Seats.Value.." seats are remaining."
                    end
                    if game.Players.LocalPlayer.PlayerGui:FindFirstChild("Confirm_Booked") then
                        game.Players.LocalPlayer.PlayerGui.Confirm_Booked:Destroy() --Use Destroy() instead, Remove() just sets the instance and it's children's parents to nil
                    end
                    child.Text = "Book"
                    child.BackgroundColor3 = Color3.new(0,255,0)
                    child.Booked.Value = false
                    child.AutoButtonColor = true
                    game.Players.LocalPlayer.Booked.Value = false
                    child.Username.Value = ""
                    if child.Name == "1C" or "2C" or "3C" or "4C" or "5C" or "6C" or "7C" or "8C" or "9C" or "10C" or "11C" or "12C" or "13C" or "14C" or "15C" or "16C" or "1B" or "2B" or "3B" or "4B" or "5B" or "6B" or "7B" or "8B" or "9B" or "10B" or "11B" or "12B" or "13B" or "14B" or "15B" or "16B" then
                        mainParts.BC.SurfaceGui[child.Name].Text = child.Name.." - Available"
                        mainParts.BC.SurfaceGui[child.Name].TextColor3 = Color3.new(0,255,0)
                    end
                    if child.Name == "1A" or "2A" or "3A" or "4A" or "5A" or "6A" or "7A" or "8A" or "9A" or "10A" or "11A" or "12A" or "13A" or "14A" or "15A" or "16A" then
                        mainParts.A.SurfaceGui[child.Name].Text = child.Name.." - Available"
                        mainParts.A.SurfaceGui[child.Name].TextColor3 = Color3.new(0,255,0)
                    end
                end
            else
                local newgui = script.Parent.Error_NoShirt:Clone()
                newgui.Parent = game.Players.LocalPlayer.PlayerGui
                newgui.Script.Disabled = false
            end
        end)
    end
end

game.Players.PlayerAdded:connect(function(plr)
    local bookedvalue = script.Parent.Booked
    local newvalue = bookedvalue:clone()
    newvalue.Parent = plr
end)
0
It should be noted that PlayerAdded does not work in LocalScripts as they run on the client. An alternative to this for a LocalScript would be ChildAdded. Spongocardo 1991 — 8y
Ad

Answer this question