How can I modify this local script to make a server side drop system

Hey,
I’m trying to finally fix this inventory system, and one of the major issue I found, is that when the player drop the tool, it is client side only, How can I make it server sided?

local equipped = script.Parent.Handler.Equipped
local selected = script.Parent.Handler.Selected
local location = script.Parent.Handler.Location
local player = game.Players.LocalPlayer
local character = player.Character

local items = {}
local buttons = {}
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false) -- Makes the original backpack gui invisible

function search(location)
	for i,v in pairs(location:GetChildren()) do -- Find all item in a specific location
		if v:isA("Tool") and #items <= 8 then -- If the item found is a "Tool"
			table.insert(items,v) -- We're going to put all the tools found in a table.
			print("1")
		end
	end
end

function refresh()
	search(location)
	for i,v in pairs(buttons) do -- Finds all items in the table
		v:Destroy() -- Destroy 'em all
		print("2")
	end
	for i,v in pairs(items) do -- Finds all items in the table
		local button = script.Sample:Clone() -- clones the sample button inside the localscript
		button.Name = v.Name -- sets the cloned button's name to the name of the item
		button.LayoutOrder = i
		button.Parent = script.Parent.Handler -- Sets the parent of the cloned button to the handler
		button.Image = v.TextureId -- Sets the image of the button to the texture id of the tool
		table.insert(buttons,button) -- Inserts the button to our table "buttons"
		print("3")
		
		button.MouseButton2Click:Connect(function()
			if v.Parent == player.Character then
				game.Players.LocalPlayer.Character:UnequipTool()
				game.Players.LocalPlayer.Character.RightHand:FindFirstChild("RightGrip"):Destroy()
				button:Destroy()
				v.Anchored = false
				v.Positon = player.Character.HumanoidRootPart.Position + Vector3.new(0.1,0,0)
				v.Parent = workspace
			elseif v.Parent == game.Players.LocalPlayer.Backpack then

				button:Destroy()
				v.Parent = workspace
				v.Positon = player.Character.HumanoidRootPart.Position + Vector3.new(0.1,0,0)

			end

		end)
		button.MouseButton1Click:connect(function()
			print("4")
			if script.Parent.Handler.Selected.Value == nil or script.Parent.Handler.Selected.Value ~= v then -- Checks if the selected value is nothing or if the selected value is not the button
				script.Parent.Frame.ItemName.Text = v.Name -- Sets the TextLabel's Text to the name of the tool/button
				script.Parent.Frame.ImageLabel.Image = v.TextureId -- Sets the image label's image to the texture id of the tool
				script.Parent.Handler.Selected.Value = v
				print("5")
				if equipped.Value == nil or equipped.Value ~= selected.Value then -- Just the same as the last one
					character.Humanoid:UnequipTools() -- Forces the player to unequip the tool that they equipped
					print("6")
					character.Humanoid:EquipTool(selected.Value)
					equipped.Value = selected.Value
					print('7')
				end
			else
				character.Humanoid:UnequipTools() -- Forces the player to unequip the tool that they equipped
				script.Parent.Handler.Selected.Value = nil
				script.Parent.Frame.ItemName.Text = "Name"
				equipped.Value = nil
			end
		end)
	end
end



function backpackRefresh()
	print("8")
	items = {}
	search(character)
	search(player.Backpack)
	refresh()
end

backpackRefresh()

player.Backpack.ChildAdded:connect(backpackRefresh)
player.Backpack.ChildRemoved:connect(backpackRefresh)
search(location)
backpackRefresh()
backpackRefresh()
1 Like

Make a RemoteEvent that handles the dropping code and Fire it whenever you need to make a tool drop

Ye I was sure that it would require a RemoteEvent.
Only thing is how I could use it, and how could I get the player item

The RemoteEvent doesn’t really need any custom parameters, just remember that it gives you the player who fired the event automatically as the first parameter. So you use that, and just change your code to suit it

In this case let’s say you named the parameter player

  • Change game.Players.LocalPlayer to `player
  • Keep the button destroy code in the localscript since it’s a local thing
  • Make a variable in the remoteevent to get the tool the player is currently using via player.Character:FindFirstChildOfClass("Tool") and use that instead of v
1 Like

Imma add this to script thanks a lot !

1 Like

Please tell me if you have any problems with doing it, and provide the code i nyour RemoteEvent if you’re having issues with it

Nah I need to do it myself, if not I will never get better. But I’ll post the script if i’m really stuck

1 Like

I jst have a question.
If I use FFCWhichIsA(“Tool”), then how can it take the item that the player want to drop

There can only be one tool in your character, the one you’re equipping, so if it finds one, that’s the one you want to drop, since in your localscript it only drops if you’re holding the tool due to the if v.Parent == player.Character then statement

Ye but in the case that the player want to drop an unequipped tool, it might take the wrong tool in the backpack

Oh right, Ididn’t see that bit, then add a parameter for the tool you want to drop. And then you can make the check there as well. If you specified a tool, that means you want a tool from your backpack to be removed, and if you didn’t specify a tool, then it means you want to get the tool from the player’s backpack, or you could alternatively just make it completely work with the parameter

Idk how to get the players, I’m really lost. Since you said how to I’m thinking about this, but Idk how to…
Here is a part of the script :

						if v.Parent == player.Character then
							game.ReplicatedStorage.DropEvent:FireServer()
							game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool")
							button:Destroy()
							v.Anchored = false
							v.Positon = player.Character.HumanoidRootPart.Position + Vector3.new(0.1,0,0)
							v.Parent = workspace

You have been asking help a lot on the Forum recently. I council watching YouTube tutorials at youtube.com, or learn like this which is okay, just a council. You could remove .LocalPlayer.

This code

game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool")
v.Anchored = false
v.Positon = player.Character.HumanoidRootPart.Position + Vector3.new(0.1,0,0)
v.Parent = workspace

Should be focused in the remoteevent, not in the localscript

Ohh thats why I was still blocked here. I’ll move this on the SSS part

Alright, got this :

local player = game:GetService("Players")
game.ReplicatedStorage.DropEvent.OnServerEvent:Connect(function()
	local t =player.Character:FindFirctChildWhichIsA("Tool")
	local humanoid = player.Character:FindFirstChild("Humanoid")
	t.Parent = workspace
	t.Position = player.Character.HumanoidRootPart.Position
	humanoid:UnequipTools()
end)

In the script

You’er doing it wrong again, you autoamtically get the player instance from OnServerevent,

game.ReplicatedStorage.DropEvent.OnServerEvent:Connect(function()

This should be this

game.ReplicatedStorage.DropEvent.OnServerEvent:Connect(function(player)

And remove

local player = game:GetService("Players")

I don’t truly understand what these word in the function mean

OnServerEvent gets you the player who fired the event as the first parameter automatically, meaning you don’t have to specify it, but it will still be given, you use that to get the player instance, not trying to the character from the Players Service

It doesn’t seems to work :face_with_raised_eyebrow: