Part that randomizes a tool when touched doesn't function properly if spawned in from ReplicatedStorage

I have a Part that when a player touches it, gets a random item. That works completely fine if the Part is in the workspace. If the Part is cloned from ReplicatedStorage, the script does not function at all and leaves no errors in the output. When the Part is cloned, it has the correct scripts in it. I’ve tried to adjust the script but nothing seems to be working

local rs = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:connect(function(p)
	p.CharacterAdded:connect(function(c)
		script.Parent.Touched:Connect(function(object)
			if object.Parent:findFirstChild("Humanoid")~=nil then
				local m = math.random(1, 3)
				if m == 1 then
					local tool1 = rs.forcefield:Clone()
					tool1.Parent = p:FindFirstChild("Backpack")
					script.Parent:Destroy()
				elseif m == 2 then
					local tool2 = rs["hologram sword"]:Clone()
					tool2.Parent = p:FindFirstChild("Backpack")
					script.Parent:Destroy()
				elseif m == 3 then
					local tool3 = rs.glasses:Clone()
					tool3.Parent = p:FindFirstChild("Backpack")
					script.Parent:Destroy()
				end
			end
		end)
	end)
end)
1 Like

Well in your script you have player added, character added and touched functions together. Do you really need the player added and the character added? You could just use GetPlayerFromCharacter in the touched function if it’s a humanoid. It might not work because the player is already in the game. And that’s because the script runs exactly after it is taken from replicated storage. In your case, you could just use a function for the touched and you don’t need much else.

2 Likes