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

This press on item for gear script wont work?


I'm making it so that when you press on an item, a gear is in a player's hand. It won't work, though, and there's nothing in the output.


SCRIPT:

Player = game.Players.LocalPlayer
TableProp = script.Parent
RageTable = game.ReplicatedStorage:WaitForChild("RageTable")

function RageTableEquipped()
	local a = RageTable:Clone()
	a.Parent = Player.Backpack	
	local b = RageTable:Clone()
	b.Parent = Player.StarterGear
end
TableProp.ClickDetector.MouseClick:connect(RageTableEquipped)

The rage table is the gear, and the script is in "CoffeeTableProp"

Thanks

--club101coolguy

2 comments

0

It needs to be a LocalScript to work

0

It is

1 answer

✓ Accepted answer

This is a very common error made in the early stages of scripting. The reason that your script doesn't work is because LocalScripts do not run if they aren't some sort of descendant of either the player's character, or player instance (such as in GUIs or BackPack). To fix this, make the script a Server Script and change the way Player is accessed. Here is what I did to tackle the problem:

function rageTable()
	return game.ReplicatedStorage:WaitForChild("RageTable"):Clone()
end

script.Parent:WaitForChild("ClickDetector").MouseClick:connect(function(player)
	rageTable().Parent = player.Backpack
	rageTable().Parent = player.StarterGear
end)

Because the argument that is given to the listening event MouseClick for ClickDetectors is the player who clicked the brick, this allows us easy access to the userdata we need. This way, the use of LocalPlayer (which only works in local scripts) isn't necessary.

2 comments

0

ServerStorage or ServerScriptService?

0

Nvm, it was in coffee table. Thx!

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