Trying to make an event that removes a tool from the player's backpack

Yet I have no clue how to reference the player in a server script or if that’s even a possibility.

local players = game:GetService("Players")

local removeSwordEvent = game.ReplicatedStorage.RemoveSword

local toolSword = game.ReplicatedStorage.ClassicSword

removeSwordEvent.OnServerEvent:Connect(function(Player)
	local player = players:GetPlayerFromCharacter(Player)
	player.Character:FindFirstChild("ClassicSword"):Destroy()
end)

As you can see in the script I tried using GetPlayerFromCharacter instead of trying to get the local player but it still gives me back “nil” due to Character.

I’m pretty new but i’m pretty sure you need to access the player’s backpack. You are currently trying to destroy “ClassicSword” inside the character model.

Hi!

Let me give you some tips about what you are trying to achieve.
If you want to destroy a players item then it could be stored in their backpack or in their character.

  • The tool is in the players backpack when the player doesn`t hold the tool in their hand.

  • But if the player activated the tool by holding it then it is in their character.

Also you already got the Player from the RemoveEvent.

removeSwordEvent.OnServerEvent:Connect(function(Player)
	local player = players:GetPlayerFromCharacter(Player)
	player.Character:FindFirstChild("ClassicSword"):Destroy()
end)

You don’t need this line:

local player = players:GetPlayerFromCharacter(Player)

Let me give you an improved script:

local players = game:GetService("Players")

local removeSwordEvent = game.ReplicatedStorage.RemoveSword

removeSwordEvent.OnServerEvent:Connect(function(Player)
	if Player.Backpack:FindFirstChild("ClassicSword") then -- checks if the tool is in the players backpack
		Player.Backpack:FindFirstChild("ClassicSword"):Destroy()
	else -- if it's not in the backpack then it must be in the character!
		Player.Character:FindFirstChild("ClassicSword"):Destroy()
	end
end)

yeah but the problem is i can’t define the character in a server script or so i think

I see, thanks. I’m still confused about functions so I wasn’t sure.

No problem.

You can always get the players character like I showed you.

Like this: (A RemoteEvent doesn’t get the character. It gets the player)

removeSwordEvent.OnServerEvent:Connect(function(Player)
    local Character = Player.Character -- Here you got the players character
end

Someday if you need the character by using a localscript then do this:

local Character = game.Players.Localplayer.Character -- Here you get the character from a localscript
  • Localplayer is the player which the localscript is running for

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.