Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

attempt to index local 'player' (nil value)?

Asked by
Clorize 31
5 years ago

so im trying to make a hitbox for a bite tool and in the 9th line (right before the "end)") it does not send the part to my character, giving me the error shown in the title

script.Parent.Activated:Connect(function(player)
    local hitbox = Instance.new("Part",game.Workspace)
    hitbox.Name = "Hitbox"
    hitbox.Anchored = true
    hitbox.CanCollide = false
    hitbox.BrickColor = BrickColor.new("Really red")
    hitbox.Transparency = 0
    hitbox.Size = Vector3.new(5,5,5)
    hitbox.CFrame = CFrame.new[player.Character.Torso.CFrame]
end)
0
I think if i use remote events it will work, if it does i will remove this question Clorize 31 — 5y
0
The Activated property does not pass a player as an argument. That's why it's not working. DeceptiveCaster 3761 — 5y
0
Activated event * and parameter* User#24403 69 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The tool.Activated event does not pass a player who activated the tool to your listener. Thus, player is nil.

If this is in a LocalScript

If this is in a local script, you can use Players.LocalPlayer

```lua local client = game:GetService("Players").LocalPlayer; local hitbox = Instance.new("Part"); --// second parent argument is deprecated and causes performance issues if misused, parent object always last

--// assign other props here

hitbox.CFrame = CFrame.new(client.Character.HumanoidRootPart.Position); hitbox.Parent = game.Workspace; ```

Another issue you have is you are indexing the new function itself and not calling it. And you cannot construct a CFrame out of a CFrame. You can, out of a Vector3 and numbers however.

CFrame.new(client.Character.HumanoidRootPart.Position);

Also R15 does not have a Torso but all body types have a HumanoidRootPart. Use that for compatibility purposes.

If this is in a Script

If this is in a server script, you can use tool.Parent to get the character when the tool is equipped.

here is an example;

```lua local tool = script.Parent;

tool.Activated:Connect(function()

local character = tool.Parent; --// This tool is going to be activated when equipped so this will be the character.

--// Create your part down here

end); ```

Ad

Answer this question