Can't get the player in a server script

Hey guys,
i’m having troubles here. I’m trying to make the damage based on how much Attack points the player have.

Here is my code:

	script.Parent.Touched:Connect(function(hit)
	
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	
	if hit and Humanoid and DamageDebounce == false then
		DamageDebounce = true
		Humanoid:TakeDamage(DefaultDamage + player.Attributes.Attack.Value)
		wait(DamageCooldown)
		DamageDebounce = false
		end
end)

For some reason i’m getting this error:
Attempt to index with Attributes

Any help is appreciated.

Are you sure you are creating whatever “Attributes” is before the player touches whatever the script is inside? I’m guessing it’s a folder containing attributes of the player. ‘Attempt to index nil with “Attributes”’ means it can’t find whatever 'Attributes is from inside the player.

1 Like

“Attributes” is a folder inside the player. “Attack” is the amount of Attack points the player have.

The problem is that to get the Attributes folder, we need the “hit”.

Can you put this line of code in the function after your if statement and see what it prints in F9 console?

print(player.Name)
1 Like

Just tried, it did print my name

Everything works except the damage. It looks like the game can’t find my Attributes folder…?

1 Like


The code seems to work fine for me, are you sure you have spelt everything correctly? Are you also sure you are parenting your ‘Attack’ IntValue to your Attributes folder? Here is my code.

local Players = game:GetService("Players")

local DamageDebounce = false
local DamageCooldown = 1
local DefaultDamage = 5

Players.PlayerAdded:Connect(function(player)
	local attributes = Instance.new("Folder")
	attributes.Name = "Attributes"
	attributes.Parent = player
	
	local attack = Instance.new("IntValue")
	attack.Name = "Attack"
	attack.Value = 5
	attack.Parent = attributes
end)

script.Parent.Touched:Connect(function(hit)

	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

	if hit and Humanoid and DamageDebounce == false then
		DamageDebounce = true
		Humanoid:TakeDamage(DefaultDamage + player.Attributes.Attack.Value)
		print("Dealt "..DefaultDamage + player.Attributes.Attack.Value.." damage")
		wait(DamageCooldown)
		DamageDebounce = false
	end
end)
1 Like

If the script is running once you start the game, the player may actually join the game before the “Players.PlayerAdded:Connect()” is called resulting in no folder being made.

I usually loop all players and run the same function before connection while in studio.

EDIT: Replied to wrong guy, sorry bout that.

This is the line for the folder.
image


And this is for the Attack value.
image

What @MEsAv2 said is right, and judging by your code above is probably what the issue is - try this

local function playerAdded(player)
	local attributes = Instance.new("Folder")
	attributes.Name = "Attributes"
	attributes.Parent = player

	local attack = Instance.new("IntValue")
	attack.Name = "Attack"
	attack.Value = 5
	attack.Parent = attributes
end

--Incase player joined earlier than this script ran
for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(playerAdded, player)
end

Players.PlayerAdded:Connect(function(playerAdded)

(Also as a side note you should not do Instance.new(“Folder”, player) if you are going to be changing attributes of the new instance as it is not optimised)

2 Likes

The attack points could be the issue.

1 Like

I don’t see anything special about it. It’s just a value inside a folder.