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

Greetings,

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
		return
	end

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


	Tool.Handle.DrinkSound:Play()

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

	Tool.GripForward = Vector3.new(-0.976,0,-0.217)
	Tool.GripPos = Vector3.new(0.3,0,0)
	Tool.GripRight = Vector3.new(0.217,0,-0.976)
	Tool.GripUp = Vector3.new(0,1,0)


	enabled = true
	
	Tool:Destroy()

end

function onEquipped()
	Tool.Handle.OpenSound:play()
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(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!