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

Why does this LOCAL Script only work on Play Solo?

Asked by
Nidoxs 190
8 years ago

This should give the clicker a tool and take away 35 Cash. If they already have the tool in their inventory then it activates a Gui. However this only works in Play Solo and it's fine in Play Solo but when I play the game it doesn't do a thing. The LOCAL script is located in the starter pack.

local Player = game.Players.LocalPlayer.Character
local Plr = game.Players.LocalPlayer
function onClicked()
if Player then
    if not Plr.Backpack:findFirstChild("Spring Rolls") then
    local a = game.ServerStorage["Spring Rolls"]:Clone()
    a.Parent = Plr.Backpack
    local amount = 35   
    local h = Player:findFirstChild("Humanoid")
    if (h~=nil) then
        local thisplr = game.Players:findFirstChild(h.Parent.Name)
        if (thisplr~=nil) then
            local stats = thisplr:findFirstChild("leaderstats")
            if (stats~=nil) then
                local Bill = stats:findFirstChild("Cash")
                if (Bill~=nil) then
                    Bill.Value = Bill.Value - amount
                end
            end
        end
        end     
    elseif Plr.Backpack:findFirstChild("Spring Rolls") then
    Plr.PlayerGui.Already.TextLabel.Script.Disabled = false
    end
    end 
end

game.Workspace["Spring Rolls"].ClickDetector.MouseClick:connect(onClicked)

1 answer

Log in to vote
0
Answered by 8 years ago

Most likely you're just getting the player's character to early. You won't often see this error in studio, because the rate the player loads is about the same as their local scripts. I suggest creating some kind of yielding method to wait for the player's character to load. Try this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait() -- returns the character when added

function onClicked()
    if Character then
        if not Player.Backpack:findFirstChild("Spring Rolls") then
            local a = game.ServerStorage["Spring Rolls"]:Clone()
            a.Parent = Player.Backpack
            local amount = 35   
            local h = Character:findFirstChild("Humanoid")
            if (h~=nil) then
                local thisplr = game.Players:findFirstChild(h.Parent.Name)
                if (thisplr~=nil) then
                    local stats = thisplr:findFirstChild("leaderstats")
                    if (stats~=nil) then
                        local Bill = stats:findFirstChild("Cash")
                        if (Bill~=nil) then
                            Bill.Value = Bill.Value - amount
                        end
                    end
                end
            end     
        elseif Player.Backpack:findFirstChild("Spring Rolls") then
            Player.PlayerGui.Already.TextLabel.Script.Disabled = false
        end
    end 
end

game.Workspace["Spring Rolls"].ClickDetector.MouseClick:connect(onClicked)

Ad

Answer this question