Help with making tool destroy on server side and not just local side

local Tool = script.Parent
local Plr = game.Players.LocalPlayer
local Used = false

Tool.Activated:Connect(function()
	local Hunger = Plr.PlayerGui:WaitForChild("HungerVal")
	if not Used then
		Used = true

		Hunger.Value = Hunger.Value + 40
		if Hunger.Value > 100 then
			Hunger.Value = 100
		end
		wait(0.5)
		script.Parent.Handle.Eat:Play()
		wait(0.5)
		Tool:Destroy()
	end
end)

Here is a local script for a food tool, at the end the tool gets destroyed, but this seems to do it only locally, and other players can still see the tool in the players hand even though it doesnt exist for them anymore. How should I go about solving this?

1 Like

You would want to have a server script with a remote event as a child additionally to the client script inside the tool, from the client script you want to fire the server script’s RemoteEvent. On the server script you put inside the tool, youll wait for the remote event to fire and then destroy the tool.

Server script inside the tool:

local Tool = script.Parent
local Event = Instance.new("RemoteEvent",script)

Event.OnServerEvent:Once(function()
Tool:Destroy()
end)

The client script should look something like this:

local Tool = script.Parent
local Plr = game.Players.LocalPlayer
local Used = false

Tool.Activated:Connect(function()
	local Hunger = Plr.PlayerGui:WaitForChild("HungerVal")
	if not Used then
		Used = true

		Hunger.Value = Hunger.Value + 40
		if Hunger.Value > 100 then
			Hunger.Value = 100
		end
		wait(0.5)
		script.Parent.Handle.Eat:Play()
		wait(0.5)
		Tool.Script.RemoteEvent:FireServer()
	end
end)
1 Like

Should also be noted that you can use :Once() on Tool.Activated, this way you won’t need to make a bool variable for if the tool was used

1 Like

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