Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

3 comments

0

I think if i use remote events it will work, if it does i will remove this question

0

The Activated property does not pass a player as an argument. That's why it's not working.

0

Activated event * and parameter*

1 answer

✓ Accepted answer

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

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;

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);

This discussion is preserved as it was. New answers, comments, and votes are closed.