Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this LOCAL Script only work on Play Solo?

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.