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

I've got a localscript that only works on studio mode. Any ideas why?

p = game.Players.LocalPlayer
repeat wait() until p:FindFirstChild("Backpack")
function Click()
	wait(2)
repeat wait() until p:FindFirstChild("Backpack")
game.ReplicatedStorage:GetChildren() [math.random(1, #game.ReplicatedStorage:GetChildren())]:clone().Parent = p.Backpack
game.ReplicatedStorage:GetChildren() [math.random(1, #game.ReplicatedStorage:GetChildren())]:clone().Parent = p.Backpack
game.ReplicatedStorage:GetChildren() [math.random(1, #game.ReplicatedStorage:GetChildren())]:clone().Parent = p.Backpack
end

script.Parent.MouseButton1Click:connect(Click)
--// Put your code in a code block. Blue button in code editor. Code in between, all that, yeah.

This is in a localscript inside of a textbutton. When the weapons are given to the player, over half of them don't work, and it only happens to players are given the weapons by this script.

1 comment

1

You need to learn how to use WaitForChild.

1 answer

p = game.Players.LocalPlayer
p:WaitForChild("Backpack") -- Waiting for the backpack before we continue our script
function Click()
game.ReplicatedStorage:GetChildren() [math.random(1, 		#game.ReplicatedStorage:GetChildren())]:clone().Parent = p.Backpack
game.ReplicatedStorage:GetChildren() [math.random(1, 	       	   #game.ReplicatedStorage:GetChildren())]:clone().Parent = p.Backpack
game.ReplicatedStorage:GetChildren() [math.random(1, 					
#game.ReplicatedStorage:GetChildren())]:clone().Parent = p.Backpack
end
script.Parent.MouseButton1Click:connect(Click)

The only thing i edited was the WaitForChild function

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