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

How to make roblox wait for local player?

Asked by 8 years ago
Edited 8 years ago

So I have a script that works fine while It's in the players startergui, it creates a giant laser that moves around and kills anyone it touches, the problem is I want it to start off in the workspace and then be moved into the local players startergui. For some reason, while it's not already in startergui, I get an error that says "Workspace.Script:3: attempt to index local 'player' (a nil value)". I think it's because Roblox hasn't loaded the local player yet. Can someone Please tell me how to fix this issue!?

Script:

-- Get the player, character, and the players gui
local player = game.Players.LocalPlayer
local char = player.CharacterAdded:wait()
local hum = char:WaitForChild("Humanoid")
local gui = player:WaitForChild("PlayerGui")

-- Delay
    wait(1)

-- Parent the script to StarterGui and set the properties of wall
    script.Parent = gui
    wall = Instance.new("Part")
    wall.Name = "wall"
    wall.Shape = "Cylinder"
    wall.Rotation = Vector3.new(0, 0, -90)
    wall.Anchored = true
    wall.Size = Vector3.new(400, 8, 8)
    wall.Transparency = 0.7
    wall.Color = Color3.new(255, 0, 0)
    wall.CanCollide = false
    wall.Material = "Neon"

    wall.CFrame = char.Torso.CFrame + char.Torso.CFrame.lookVector * 5  
    wall.CFrame = wall.CFrame * CFrame.Angles(0, 0, 1/2 * math.pi)  
    wall.Parent = game.Workspace

-- Move the wall
    for i=1, 500 do

        -- If the wall hits someone, kill them
        function hited(hit)
            local hum = hit.Parent:FindFirstChild("Humanoid")
                if hum then
                    hum.Health = 0
                end
            end

        wall.Touched:connect(hited)
        -- Set the walls position
        wait()
        wall.CFrame = wall.CFrame + char.Torso.CFrame.lookVector * 5 + Vector3.new(0, 0, -1)        

        -- Destroy the wall eventually
        if i >= 500 then        
            wall:destroy()
        end

        -- If you get killed by the wall, it removes itself
        if hum.Health == 0 then
            wall:destroy()
        end
    end

2 answers

Log in to vote
0
Answered by 8 years ago

The problem is that Local Scripts only work in certain areas. The local player can only be found inside the Player, but Workspace isn't a player so it returns 'nil' as a way of saying "Hey I am not inside a Player!"

Instead of using LocalPlayer...I would just do a regular script in the workspace with the 'PlayerTouched' event... it will give you the player touching the wall so that the event automatically sets wh touched it.

Hope this helps!

Ad
Log in to vote
0
Answered by 8 years ago

repeat wait() until game.Players.LocalPlayer

Answer this question