So i'm using scripts from a tutorial I watched about how to make tools get picked up by pressing E (https://www.youtube.com/watch?v=LtQmj3YgLnE&t=(you don't have to watch the whole thing to understand my problem)). It works perfect normally, but I added some code to it for throwing the tool if you press E again but while holding a tool. I noticed that just parenting the tool to the workspace like when you press backspace to drop it makes it just float in the air for a bit before the script makes it move, so I had to clone it instead to make it work. Here's the code for throwing the tool if that helps:
if player.tool.Value == true then
local tool = char:FindFirstChildWhichIsA("Tool")
local Handle = tool.Handle
throwAnimationTrack:Play()
throwAnimationTrack:GetMarkerReachedSignal("throwWeapon"):Connect(function()
local direction = char.LeftHand.CFrame.LookVector
local orientation = tool.Handle.Orientation
local thrownTool = tool:Clone()
tool:Destroy()
thrownTool.BodyAngularVelocity.AngularVelocity = Vector3.new(0,0,10)
thrownTool.Handle.CanCollide = true
thrownTool.Handle.Position = char.RightHand.Position + direction * 2.5
thrownTool.Parent = workspace.tools
local Thrown = true
thrownTool.Handle.Touched:Connect(function(part)
local partParent = part.parent
local otherHumanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if(otherHumanoid and Thrown == true) then
if(part.Name == "Head") then
otherHumanoid:TakeDamage(40)
else
otherHumanoid:TakeDamage(20)
end
Thrown = false
recoilModule.Recoil(partParent)
end
Thrown = false
end)
while (Thrown == true) do
thrownTool.Handle.Velocity = Vector3.new(direction.x,direction.y -0.4,0) * 70
wait()
end
end)
player.tool.Value = false
else
I realized that after you throw the tool, you are unable to pick it back up again, so I put some print statements to try to find out why this was happening:
in local script
if tool then
print(tool)
event:FireServer(tool)
ToolImage.Visible = false
end
in server script
re.OnServerEvent:Connect(function(plr, tool)
print(tool)
local mag = (tool:WaitForChild("Handle").Position - plr.Character.HumanoidRootPart.Position).magnitude
if mag < 5 then
tool.Parent = plr.Backpack
end
end)
Normally, both of these print statements print "tool" like they're supposed to because that's the name of the tool. However, after you throw the weapon, only the first print in the part where it fires the event prints tool. The other one prints nil. Somehow, even though i'm putting the tool into the parameters, it comes out on the other script as nil. I am pretty sure this is because I cloned the tool (if not that then probably something else in the code for throwing it). Can someone tell me why this is, or how i can throw the tool without having to clone it?

1 comment
good old youtube