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

Why is this script to detect an object in the players character broken?

Asked by 9 years ago

So I have a tool out in the workspace, and noticed that it gets cloned into the players character when its picked up (not the backpack) I'm not sure why this happens but I went with it and created this script.

It works fine in solo but not online. Anyone know why?

while true do
    wait(.1)
    local plr = game.Players.LocalPlayer
    local player = plr.Name
    local PlayerInventory = game.Workspace:FindFirstChild(player)
    local val = script.Parent.Value.Value
    local ObjFrame = plr.PlayerGui.Objectives.Frame

    if PlayerInventory:FindFirstChild("Flashlight") and val == 1 and ObjFrame.Visible == true then -- error is that PlayerInventory is a nil value


-- The rest of this script was just added to this code block for completion
        val = 0
        ObjFrame.Objective.Text = "Objective Complete"
        ObjFrame.Flavour.Text = ""
        for i = 1,200,5 do
            ObjFrame.Objective.TextColor3 = Color3.new (1-i/255,1,1-i/255)  
            wait()
        end
        wait(4)
        for i = 1,100,2 do
            ObjFrame.Flavour.TextTransparency = i/100
            ObjFrame.Objective.TextTransparency = i/100
            ObjFrame.TextLabel.TextTransparency = i/100
            wait()
        end
        ObjFrame.Visible = false
        ObjFrame.Flavour.TextTransparency = 1
        ObjFrame.Objective.TextTransparency = 1
        ObjFrame.TextLabel.TextTransparency = 1
        ObjFrame.Objective.TextColor3 = Color3.new (0, 0,0) 
    end
end

2 answers

Log in to vote
0
Answered by
TopDev 0
9 years ago

I think the script is executing before the player's character is added, making the PlayerInventory a nil value, try this script, I've made it wait for the character.

while true do
    wait(.1)
    local plr = game.Players.LocalPlayer
    local player = plr.Name
    plr.CharacterAdded:wait()
    local PlayerInventory = game.Workspace:FindFirstChild(player)
    local val = script.Parent.Value.Value
    local ObjFrame = plr.PlayerGui.Objectives.Frame

    if PlayerInventory:FindFirstChild("Flashlight") and val == 1 and ObjFrame.Visible == true then -- error is that PlayerInventory is a nil value


-- The rest of this script was just added to this code block for completion
        val = 0
        ObjFrame.Objective.Text = "Objective Complete"
        ObjFrame.Flavour.Text = ""
        for i = 1,200,5 do
            ObjFrame.Objective.TextColor3 = Color3.new (1-i/255,1,1-i/255)  
            wait()
        end
        wait(4)
        for i = 1,100,2 do
            ObjFrame.Flavour.TextTransparency = i/100
            ObjFrame.Objective.TextTransparency = i/100
            ObjFrame.TextLabel.TextTransparency = i/100
            wait()
        end
        ObjFrame.Visible = false
        ObjFrame.Flavour.TextTransparency = 1
        ObjFrame.Objective.TextTransparency = 1
        ObjFrame.TextLabel.TextTransparency = 1
        ObjFrame.Objective.TextColor3 = Color3.new (0, 0,0) 
    end
end

Ad
Log in to vote
0
Answered by
Defaultio 160
9 years ago

Make sure this is a LocalScript.

Answer this question