Tool invisible to other players

Hello. I want to make a weapon that you can choose from the lobby. The issue is when I spawn in, it can’t be seen to other players nor I can see others’. I’ve tried using remote events to fix it.

Original script:

local touched = false
function touch()
	local Player = game.Players.LocalPlayer
	if not Player.Backpack:FindFirstChildWhichIsA("Tool") then
		wait(1)
		print(Player)
		print(Player.PlayerGui)
		print(Player.PlayerGui.Weapons)
		print(Player.PlayerGui.Weapons.RockEquipped)
		print(Player.PlayerGui.Weapons.RockEquipped.Value)
		print(Player.PlayerGui.Weapons.RockEquipped.Value == true)
		if Player.PlayerGui.Weapons.RockEquipped.Value == true then
			game.Workspace.Lobby.Show.Rock.Handle.Anchored = false
			game.Workspace.Lobby.Show.Rock.Handle.Transparency = 0
			game.Workspace.Lobby.Show.Rock.Parent = Player.Backpack
		end 	
	end	
end	
game.Workspace.SpawnIn.Touch.Touched:Connect(function()
	if not touched then
		touched = true
		touch()
		wait(2)
		touched = false
	end		
end)

Localscript to fire the remote event:

local activ = false
game.Workspace.SpawnIn.Touch.Touched:Connect(function(hit)
	if not activ then
		if game.Players.LocalPlayer.PlayerGui.Weapons.RockEquipped == true then
			print("fired")
			activ = true
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			game.ReplicatedStorage.PlayerTouch:FireServer()
			wait(1)
			activ = false
		end
	end
end)

Serverscript that connects to the remote event:

local activated = false
script.Parent.Touched:Connect(function(hit)
	if not activated then
		activated = true
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		game.ReplicatedStorage.PlayerTouch.OnServerEvent:Connect(function(player)
			game.Workspace.Lobby.Show.Rock.Handle.Anchored = false
			game.Workspace.Lobby.Show.Rock.Handle.Transparency = 0
			game.Workspace.Lobby.Show.Rock.Parent = Player.Backpack
		end)
		wait(1)
		local destination = game.Workspace.Spawns:GetChildren()
		if Player then
			Player.Character.HumanoidRootPart.Position = Vector3.new(-16.44, 25.543, -4.493)
		end
		wait(1)
		activated = false
	end
end)

And here is how everything is set up: Game > StarterGui > RockEquipped (Original script),
Game > StarterGui > PlayerTouch (Localscript to fire remote event),
Game > SpawnIn > Touch > Script (Script that connects the remote event),
Game > Replicated Storage > PlayerTouch (This is the remote event).

Edit: I’ve tried turning line 13, 14 and 15 in my original script into a comment. Now I don’t get a tool at all so the remote event isn’t working but I don’t see a problem with the scripting.

1 Like

Try using Script and not LocalScript for firing the RemoteEvent. LocalScript is for Client side and not Server side.

Yeah, but only a local script can use :FireServer

Yeah you’d use a local script to read player input, and then fire the information to the server to handle it.

1 Like

A client script, that is. That’s the problem. Should it be visible for everyone, considering what this accomplishes? The FireServer part?

An alternative is FireClient(player: Player), inside a ServerScript, or FireAllClients(), which is also inside a ServerScript.

Where do I put the ServerScript? I don’t think it works in ServerScriptService.

I’ve changed some:
Original Script

local touched = false
function touch()
	local Player = game.Players.LocalPlayer
	if not Player.Backpack:FindFirstChildWhichIsA("Tool") then
		if Player.PlayerGui.Weapons.RockEquipped == true then
			wait(1)
			print(Player)
			print(Player.PlayerGui)
			print(Player.PlayerGui.Weapons)
			print(Player.PlayerGui.Weapons.RockEquipped)
			print(Player.PlayerGui.Weapons.RockEquipped.Value)
			print(Player.PlayerGui.Weapons.RockEquipped.Value == true)
			script.Parent.Touched:Connect(function(hit)
				--local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
				game.ReplicatedStorage.PlayerTouch.OnClientEvent:Connect(function()
					game.Workspace.Lobby.Show.Rock.Handle.Anchored = false
					game.Workspace.Lobby.Show.Rock.Handle.Transparency = 0
					game.Workspace.Lobby.Show.Rock.Parent = Player.Backpack
				end)			
			end)	
		end	
	end
end	
game.Workspace.SpawnIn.Touch.Touched:Connect(function()
	if not touched then
		touched = true
		touch()
		wait(2)
		touched = false
	end		
end)

Event Fire:

local activ = false
game.Workspace.SpawnIn.Touch.Touched:Connect(function(hit)
	if not activ then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Player.PlayerGui.Weapons.RockEquipped == true then
			print("fired")
			activ = true
			game.ReplicatedStorage.PlayerTouch:FireAllClients()
			wait(1)
			activ = false
		end
	end
end)

But it still doesn’t work correctly. I don’t get the tools on spawn.

Turns out I did everything good, I just forgot to add .Value at:
if Player.PlayerGui.Weapons.RockEquipped == true then

still doesn’t work I’m a failure

Hey, don’t call yourself a failure. Even a professional programmer always need to debug their code in order for it to work.

1 Like

I wanna ask, can you please tell us which script is local and which is server?

It was a joke. I’m trying to fix it.

the long script is local and the short is server

What does each script do? Can you explain more?

I’m trying to fix it on my own, I’ll come back later if I need help.

Okay, feel free to ask any questions here if you need help.

Ok, let’s break down your scripts into what you want them to do and how that should be implemented.

From what I understood, this is what you want to do:

  1. When the player touches the SpawnIn part, if they have the Rock weapon equipped, they should be given it.

But how can we implement this?

  1. First of all, we need to detect when the player touches the SpawnIn part. That much is easy enough. Simply use the .Touched event.

  2. Next, we need to detect if the player has the rock item equipped on the UI. For this reason, the above code from 1. and 2. must be done in a Client Script (LocalScript or Script with Client RunContext)

We can now tell the server that we want to get this item. It is also a good idea to make sure the player owns this item (different form equipping) so that exploiters can’t cheat items in. I’ll leave that part to you.

You can place a EquipTool RemoteEvent in replicated storage. It is also a good idea to use :WaitForChild on the localscript.

In the server:

  1. Detect when EquipTool is fired from the client via .OnServerEvent:Connect.

  2. Check the player owns the tool (up to you)

  3. Make sure the player doesn’t already have the tool equipped to prevent duplicates. For examples’ sake, we can use :FindFirstChild(name) to make sure it isn’t already there bur for future cases I recommend using CollectionService

  4. Remember to CLONE the tool do NOT simply place it into the Player’s inventory. Place the tool into ServerStorage or somwhere inaccessible to the client. It is also not necessary to change the transparency. Simply use :Clone on the tool and set the parent to the player’s backpack. That’s it.

Another note: The first argument to .OnServerEvent is the player. Always. You do not need to pass it in yourself.

It works now but that created another big problem. Whenever a player spawns in, the first player that spawned in get multiple weapons.