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

Why can't Character be found in LocalPlayer when in game?(It will work in normal studio test)

Asked by 8 years ago

Hello! I am currently working on an interactive test and everything works great! However, I realized I never tested it in actual game mode. And for some reason I get this in the server output:

Players.Player1.PlayerGui.MidnaGui.Frame.ImageButton.LocalScript:2: attempt to index field 'Character' (a nil value) 15:04:41.272 - Stack Begin 15:04:41.272 - Script 'Players.Player1.PlayerGui.MidnaGui.Frame.ImageButton.LocalScript', Line 2 15:04:41.273 - Stack End

I have the script in the Player's PlayerGui>MidnaGui>Frame>ImageButton>LocalScript

Mid = game.ServerStorage:FindFirstChild("Midna Anim")
prt = game.Players.LocalPlayer.Character:WaitForChild("Par2")--This is where it supposedly can't find Character within Local Player
num = game.ServerStorage.NumValue
gui = script.Parent.Parent.Parent
txt = gui.Frame2.TextBox
plr = game.Players.LocalPlayer
txt.Text = "What is it, " ..plr.Name .."?"

script.Parent.MouseButton1Click:connect(function()
    if num.Value == 1 then
    prt.Parent.Head.Apear:Play()
    Mid.Parent = game.Workspace
    Mid:MoveTo(prt.Position)
    gui.Frame2.Visible = true
    num.Value = 2
    elseif num.Value == 2 then
        if prt.Parent.Head.Apear.isPlaying == true then
        prt.Parent.Head.Apear:Stop()
        end 
    prt.Parent.Head.Vanish:Play()   
    Mid.Parent = game.ServerStorage
    gui.Frame2.Visible = false
    num.Value = 1
    end

end)

I'm not sure why it can't find the character in game but can in studio. Any help?

Thanks! ~Minikitkat

0
Are you using a server or local script? Lacryma 548 — 8y
0
A LocalScript minikitkat 687 — 8y

1 answer

Log in to vote
1
Answered by
Lacryma 548 Moderation Voter
8 years ago

Before we tackle the main problem, there is something you need to fix.

In a local script you cannot access ServerStorage.

I believe the only reason you were able to access this initially is because you ran this inside of studio with play solo.

Instead of ServerStorage, use ReplicatedStorage which will allow clients to access the objects in it.

Solution:

Now the main problem is that you are attempt to use the character while it has not been loaded yet.

The simple solution is to wait until the character is loaded, it is a one liner.

repeat wait() until game.Players.LocalPlayer.Character
0
Alright! Thanks! minikitkat 687 — 8y
Ad

Answer this question