I've been having trouble trying to figure out why my code isn't working, basically- I'm trying to make that if it is touched by a player, it will be put into their inventory.
Code:
01 | tool = script.Parent |
02 | Handle = tool.Parent |
03 |
04 |
05 | enabled = false |
06 | script.Parent.Touched:Connect( function (OnTouched) |
07 | h = script.Parent.Players.PlayerAdded:FindFirstChild( "Humanoid" ) |
08 | if h = = true then |
09 | if debounce = = false then |
10 | debounce = true |
11 | tool.Equipped:Connect( function () |
12 | if h = = false then |
13 | return |
14 | end |
15 | end ) |
Although the tool itself does not have a Touched() event, the handle does. You almost have it just a few adjustments needed.
01 | Tool = script.Parent -- Define your tool. |
02 | Handle = Tool:WaitForChild( 'Handle' ) -- Define the thing that needs to be touched, in this case your handle. |
03 |
04 | Players = game:GetService( 'Players' ) |
05 |
06 | Handle.Touched:Connect( function (otherPart) |
07 | local Player = Players:GetPlayerFromCharacter(otherPart.Parent) |
08 | if Player then -- Check if the thing touching it is an actual player. |
09 | Tool.Parent = Player.Backpack -- Parent the tool to their inventory/backpack. |
10 | end |
11 | end ) |
There no debounces in the script, so removes the debounce.
Next, some of the if-then
and functions if
doesn't have an end, so add it.
So basically the script is like this:
01 | tool = script.Parent |
02 | Handle = tool.Parent |
03 |
04 | enabled = false |
05 |
06 | script.Parent.Touched:Connect( function (OnTouched) |
07 | h = script.Parent.Players.PlayerAdded:FindFirstChild( "Humanoid" ) |
08 | if h = = true then |
09 | tool.Equipped:Connect( function () |
10 | if h = = false then |
11 | return |
12 | end |
13 | end ) |
14 | end |
15 | end ) |
I hope this helps you! Bye bye~~~