Help me find the player for this script! Script inside a tool located in ReplicatedStorage


May someone please help me with getting the player? This is a script inside of a tool btw.
Script is shown down below:

If you are wondering what I am trying to do on my script is that I want to script a system that gives energy to the player when he eats an item.

I assume that “player” has not been defined in this script.

1 Like

I mean, why would I have asked if it was? Also “player” has a red underline.

Do you mind showing the whole script? I can’t really help you with only this peice.

1 Like
local Tool = script.Parent;

enabled = true

function onActivated()
	if not enabled  then

	enabled = false
	Tool.GripForward =,0,-0.217)
	Tool.GripPos =,-0.76,1.4)
	Tool.GripRight =,0, 0.976)
	Tool.GripUp =,1,0)


	local energy = player:WaitForChild("leaderstats"):WaitForChild("Energy")
	energy = energy + 10
	print(player.. " has received 10 energy from eating an apple. " " more to go!")
	if energy > 100 then
		energy = 100

	Tool.GripForward =,0,-0.217)
	Tool.GripPos =,0,0)
	Tool.GripRight =,0,-0.976)
	Tool.GripUp =,1,0)

	enabled = true


function onEquipped()


Alright, so, to get the player, I would simply add a line like this towards the beginning:

local player = script:FindFirstAncestorOfClass("Player") or game.Players:GetPlayerFromCharacter(Tool.Parent)
1 Like

Alright, I will add it and come back with the results.

Yea it worked just fine, but I got this error which I have never seen before (it is not related to player thing)

change this to

energy.Value = energy.Value + 10

You only got the energy instance/number value, but you tried to add a number to the instance itself and not the value.

1 Like

I somehow missed that, I don’t know why I have forgotten the .Value when I made the local energy thing, thanks a lot. But how do I delete my tool since I don’t know if Tool:Destroy() works. Oh ok nvm it works just fine.

I want to thank you once again for helping me with this, I really appreciate it!