1 | local function partTouched(onTouch) |
2 | local tool = game.Players:FindFirstChild( "Tool" ) |
3 | if tool.Name = = "Sword" then |
4 | tool:Destroy() |
5 | end |
6 | end |
7 |
8 | script.Parent.Touched:Connect(partTouched) |
I made this script to remove your sword when you touch this part It doesn't work for some reason and here is the output error:
11:30:44.415 - Workspace.Part.Script:3: attempt to index nil with 'Name'
In your case, it means that "tool" does not exist. Also, tools go inside the player's backpack, not inside the player object. I assume your code should look something like this below.
01 | local function partTouched(hit) |
02 | local player = game:GetService( "Players" ):GetPlayerFromCharacter(hit.Parent) |
03 | if player then |
04 | local tool = player.Backpack:FindFirstChild( "Sword" ) |
05 | if tool then |
06 | tool:Destroy() |
07 | end |
08 | end |
09 | end |
10 |
11 | script.Parent.Touched:Connect(partTouched) |
It means your trying to check for a name and trying to change the name to that specific name to fix this try:
01 | local function partTouched(onTouch) |
02 | local player = game.Players.LocalPlayer |
03 | local Tool = player.Backpack:FindFirstChild( "Tool" ) --Find the tool name |
04 | if Tool then |
05 | if Tool.Name = = "Sword" then |
06 | tool:Destroy() |
07 | end |
08 | end |
09 | end |
10 | end |
11 | script.Parent.Touched:Connect(partTouched) |