I am trying to make a Tool dropping script. It works but if the player isn’t holding the tool when they drop it they can’t pick it back up but if they are holding it when they drop it they can pick it up. The code is a local script and the code is in the tool.
The Code
local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then
local value = inputObject.KeyCode.Value
if value == 113 and Tool.Parent.Name == "Backpack" or value == 113 and Tool.Parent:FindFirstChild("Humanoid") then
Tool.Parent = workspace
end
end
end)
The issue is that the tool is being dropped locally, and consequently, it does not exist on the server and only for the player/client (this also causes the weird inability to get the tool back, if the tool was on the backpack.), since you are changing the parent property from a local script.
You need to add a remote, (RemoteEvent preferably, like @Tonnumkung4 mentioned.) put it in the tool and also add a normal script along with it that listens to the event and drops the tool. (use RemoteEvent:FireServer() on the local script, and RemoteEvent.OnServerEvent() on the normal script.)
Oh and I adjusted the backpack and character checks for you.
The scripts should be like these:
Local Script
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local Tool = script.Parent
local DropRemote = Tool.Drop --you can change the name of the remote to anything else if you want to
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then
local value = inputObject.KeyCode.Value
if value == 113 and Tool.Parent == Player.Backpack or value == 113 and Tool.Parent == Character then
DropRemote:FireServer(Player)
end
end
end)
Server/Normal Script
local Tool = script.Parent
local DropRemote = Tool.Drop
DropRemote.OnServerEvent:Connect(function(Player)
if Tool.Parent ~= workspace then
Tool.Parent = workspace
end
end)
At last, it probably should look like this from the explorer: