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.
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)
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:
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?
First of all, we need to detect when the player touches the SpawnIn part. That much is easy enough. Simply use the .Touched event.
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:
Detect when EquipTool is fired from the client via .OnServerEvent:Connect.
Check the player owns the tool (up to you)
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
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.