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