How do i get the player from a tool hit event

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    When a Axe hits the tree I want to find the player who used the axe and increase a leaderstat

  2. What is the issue? Include screenshots / videos if possible!
    I couldn’t find anything about this.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I couldn’t find anything about this.

local x = z.Part 
local y = z.TreeMeshTop 
local debounce = false
local hits = 0
local hitsN = 8
x.Touched:Connect(function(hit) 
	if hit.Parent.Name == "Axe" then 
		if not debounce then
			debounce = true
			hits = hits + 1
			wait(2)
			debounce = false
		
		
		if hits == hitsN then
			hits = 0
			x.Anchored = false 
			y.Anchored = false
			y.CanCollide = false 
			wait(4)
			x:Destroy()
			y:Destroy()
			local tree = game.ServerStorage["Animated Tree"]:Clone()
			tree.Parent = game.workspace
			wait(0.01)
			z:Destroy()
			end
		end
	end
end)

This is the current script (Server Script) inside the tree.

Since the tool is equipped you can get the character from the tools parent.

local character = hit.Parent.Parent

Then you can use players get player from character to get the player.

1 Like

I don’t think this is a tool script, since there are no references to any of the events shared by Tool instances.

local z = script.Parent --im assuming this is the scripts parent
local x = z.Part
local y = z.TreeMeshTop 
local debounce = false
local hits = 0
local hitsN = 8
local players = game:GetService("Players")

x.Touched:Connect(function(hit)
	if debounce then
		return
	end
	if hit.Parent.Name == "Axe" then
		local char = hit.Parent:FindFirstAncestorOfClass("Model")
		local plr = players:GetPlayerFromCharacter(char)
		local leaderstats = plr:WaitForChild("leaderstats")
		local stat = leaderstats:WaitForChild("Stat") --change to name of stat
		stat.Value += 1 --change value by amount (you can move this to later inside the callback function if necessary)
		debounce = true
		hits += 1
		task.wait(2)
		debounce = false
		if hits == hitsN then
			hits = 0
			x.Anchored = false 
			y.Anchored = false
			y.CanCollide = false 
			task.wait(4)
			x:Destroy()
			y:Destroy()
			local tree = game.ServerStorage["Animated Tree"]:Clone()
			tree.Parent = game.workspace
			wait(0.01)
			z:Destroy()
		end
	end
end)

To the original poster, I had to define something for the variable named “z” because before it was undeclared, you should probably change what I used however (I just guessed that it might be the script’s parent).

local Player = hit.Parent.Parent.Parent

That should declare the Player.

1 Like

Thank you, It works! But I don’t really understand what the task in wait() is for, can you please explain?
Also sorry I accidentally didn’t include the z variable in the paste :sweat_smile:

Visit the Task Library for more!

1 Like

Ah ok I get it now, Thank you. I’ll use this instead of the global wait.

You’re very welcome, have a good one!

1 Like