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

Workspace.tecknobladee.Punch.Handle.Damage:6: attempt to index nil with 'leaderstats'?

Asked by 4 years ago

I want to make it so every rebirth you gain 0.25 dmg but an error occurred.

15:10:43.471 - Workspace.tecknobladee.Punch.Handle.Damage:6: attempt to index nil with 'leaderstats'

script.Parent.Touched:Connect(function(hit, player)
    local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
    if humanoid then
        humanoid:TakeDamage(player.leaderstats.rebirths.Value / 4 + 5)
    end
end)

2 answers

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

The Touched event only returns one parameter, and that the thing that hit the part. It doesn't return the player that touched the part. Since you're already checking if the thing that hit the part is a character, you can get the player from the character if it has one by using the Players:GetPlayerFromCharacter function.

EDIT: You can get the player's Character that is holding the tool by just using script.Parent's. Then you can use the Players:GetPlayerFromCharacter function to get the player from the Character.

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
    if humanoid then
        local CurrentCharacter = script.Parent.Parent.Parent -- You may need to change how many parent's you need to reach the Character
        local CurrentPlayer = Players:GetPlayerFromCharacter(CurrentCharacter)
        humanoid:TakeDamage(CurrentPlayer.leaderstats.rebirths.Value / 4 + 5)
    end
end)

Hope this helps.

0
So how should I fix it? Tecknobladee 4 — 4y
0
I edited my answer with the fixed code. Hope it helps. xInfinityBear 1777 — 4y
0
It says nil with leaderstats on number 7 Tecknobladee 4 — 4y
0
Are you sure leaderstats exist inside the player? Try making variables before the TakeDamage function for the leaderstats xInfinityBear 1777 — 4y
View all comments (11 more)
0
oh ok thank you Tecknobladee 4 — 4y
0
I tried making variables but it keeps on looking in workspace. how do i make it so it looks in the players files/ Tecknobladee 4 — 4y
0
What do you mean it keeps looking in the workspace? It shouldn't be looking in the workspace. Can you send me a screenshot of your code? xInfinityBear 1777 — 4y
0
local Players = game:GetService("Players") script.Parent.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid') if humanoid then local player = Players:GetPlayerFromCharacter(hit.Parent) local rebirths = player.leaderstats.rebirths humanoid:TakeDamage(rebirths.Value / 4 + 5) end end) Tecknobladee 4 — 4y
0
I putted it as a answer Tecknobladee 4 — 4y
0
Okay.. Are you trying to get the leaderstats from the player that was hit by the handle or from the player that is holding the handle? xInfinityBear 1777 — 4y
0
from the player is holding Tecknobladee 4 — 4y
0
Ahh, okay that makes sense. My bad. I'll edit my answer. xInfinityBear 1777 — 4y
0
Okay, I've edited my answer. Hope it works now. xInfinityBear 1777 — 4y
0
Thanks it worked Tecknobladee 4 — 4y
0
No problem. Make sure to mark my answer as accepted. xInfinityBear 1777 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
        local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
        if humanoid then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        local rebirths = player.leaderstats.rebirths
            humanoid:TakeDamage(rebirths.Value / 4 + 5)
        end
    end)

Answer this question