✓ Accepted answer
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
local CurrentPlayer = Players:GetPlayerFromCharacter(CurrentCharacter)
humanoid:TakeDamage(CurrentPlayer.leaderstats.rebirths.Value / 4 + 5)
end
end)
Hope this helps.
15 comments
So how should I fix it?
I edited my answer with the fixed code. Hope it helps.
It says nil with leaderstats on number 7
Are you sure leaderstats exist inside the player? Try making variables before the TakeDamage function for the leaderstats
oh ok thank you
I tried making variables but it keeps on looking in workspace. how do i make it so it looks in the players files/
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?
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)
I putted it as a answer
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?
from the player is holding
Ahh, okay that makes sense. My bad. I'll edit my answer.
Okay, I've edited my answer. Hope it works now.
Thanks it worked
No problem. Make sure to mark my answer as accepted.