I don’t get it? one of my games has a script that drops tools and can be picked up when touching it, unless a TouchTransmitter or TouchInterest often appears inside the Handle, but not for this one…
Any advice?
local function dropRandomTool(character)
local tools = {}
for _, item in ipairs(LocalPlayer.Backpack:GetChildren()) do
if item:IsA("Tool") and item.CanBeDropped then
table.insert(tools, item)
end
end
for _, item in ipairs(character:GetChildren()) do
if item:IsA("Tool") and item.CanBeDropped then
table.insert(tools, item)
end
end
if #tools > 0 then
local randomIndex = math.random(#tools)
local toolToDrop = tools[randomIndex]
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local forwardVector = humanoidRootPart.CFrame.LookVector
local dropPosition = humanoidRootPart.Position + forwardVector * 4
toolToDrop.Parent = game.Workspace
toolToDrop.Handle.CFrame = CFrame.new(dropPosition)
end
end
LocalPlayer.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild(“Humanoid”)
local previousHealth = humanoid.Health
humanoid.HealthChanged:Connect(function(health)
if health < previousHealth then
dropRandomTool(character)
end
previousHealth = health
end)
Okay, so after thinking about this in a matter of minutes, I think it’s either that the code is written in LocalScript, the code is written in a Script but set into a client, OR the handle doesn’t receive TouchInterest…? I am not too sure about this
EDIT: I actually can’t pick up a tool that is moved from a backpack or is unequipped SPECIFICALLY, it’s still an issue to me though.
local randomIndex = math.random(#tools)
local toolToDrop = tools[randomIndex]
I think this is where the problem is, math.random requires two arguments. It picks a range of numbers and you aren’t reading from the table either, you’re getting a random number from 8 to nothing.
This is what you should do:
local randomIndex = tools[math.random(1, #tools)] -- Picks a random number from 1 to the length of the array, and it's also reading from the table so it picks a random index.
local toolToDrop = tools[randomIndex]
Unfortunately, no effect, it still drops without it, the only issue is the Handle didn’t receive a TouchInterest for players to pick up a dropped tool when the tool moved to the workspace… and sure I’ll move this topic somewhere!
Can’t you just drop a tool by clicking backspace anyway? And you can pick it up by just walking over to it. Roblox does that by default. Does your tool have a part named Handle?
WAIT! i see the issue, you can’t do this in a local script, switch it to a server script.
Plus keep this code:
local randomIndex = tools[math.random(1, #tools)] -- Picks a random number from 1 to the length of the array, and it's also reading from the table so it picks a random index.
local toolToDrop = tools[randomIndex]
local function dropRandomTool(character)
local tools = {}
-- Collect tools in the character
for _, item in ipairs(character:GetChildren()) do
if item:IsA("Tool") and item.CanBeDropped then
table.insert(tools, item)
end
end
-- Drop a random tool if there are any
if #tools > 0 then
local randomIndex = math.random(#tools)
local toolToDrop = tools[randomIndex]
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local forwardVector = humanoidRootPart.CFrame.LookVector
local dropPosition = humanoidRootPart.Position + forwardVector * 4
toolToDrop.Parent = game.Workspace
toolToDrop.Handle.CFrame = CFrame.new(dropPosition)
-- Add touch interest
local touchInterest = Instance.new("TouchInterest", toolToDrop.Handle)
end
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
– Bind the function to drop tools to the character
local humanoid = character:WaitForChild(“Humanoid”)
local previousHealth = humanoid.Health
humanoid.HealthChanged:Connect(function(health)
if health < previousHealth then
dropRandomTool(character)
end
previousHealth = health
end)
end)
-- Drop tools when the character is added
if player.Character then
dropRandomTool(player.Character)
end
fix the math.random thing too xd. Oh yeah and put the character:GetChildren loop under the characteradded event so OP doesn’t get confused if he errors again. Edit: just realised for some reason math.random works even with just one argument, you learn something new everyday.
-- Server script (parent this to startercharacterscripts)
local char = script.Parent
char.Humanoid:GetPropertyChangedSignal("Health"):Connect(function(property)
if char.Humanoid.Health < 100 then
local tools2 = game.Players:GetPlayerFromCharacter(script.Parent).Backpack:GetChildren()
local randomindex2 = tools2[math.random(1, #tools2)]
randomindex2.Parent = workspace
end
end)
Actually do this new script I gave all server-side. I edited it, no need to do from the client. Sorry I edited it twice since I realised it wouldn’t have worked kind of. Edited again lol. Yeah I realised it didn’t work give me a second.
I think you might wanna test out the script twice (or more if you found it buggy) that you remodified before re-editing the message, you still have time, thank you for helping this out by the way! (-:
By the way do you just want the tool that the player is holding to drop? Or both the holding tool and the tools in the player’s backpack to drop randomly?
Both the holding tool and the tools in the player’s backpack to drop randomly, it’ll be pretty good for snitching each other pretty easily for my game!
-- Local script (Parent to startercharacterscripts)
local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local char = script.Parent
char.Humanoid:GetPropertyChangedSignal("Health"):Connect(function(property)
if char.Humanoid.Health < 100 then
RE:FireServer()
end
end)
-- Server script (Serverscriptservice)
local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent")
RE.OnServerEvent:Connect(function(plr)
local tools = {}
for i, tool in plr.Character:GetChildren() do
if tool:IsA("Tool") then
table.insert(tools, tool)
end
end
for i, tool1 in plr.Backpack:GetChildren() do
if tool1 then
table.insert(tools, tool1)
end
end
print(#tools)
if #tools > 1 then
local randomindex = tools[math.random(1, #tools)]
randomindex.Handle.Name = "NoHandle"
randomindex.Parent = workspace
end
if #tools == 1 then
tools[1].Handle.Name = "NoHandle"
tools[1].Parent = workspace
end
end)
Also this will happen everytime the player loses/gains health. So to fix that issue I added an empty script in startercharacterscripts and named it “Health”. Do that too, it’ll stop players from healing. But if you want healing to be part of your game you will have to replace this event with:
while char.Humanoid.Health -= 1 do
-- The same code
end
The reason I chose not to this was because I thought it may’ve been impactful on memory. But it’s your choice.