Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

So I can't find the Player from Character?

Well all I want is this tool to destroy itself after one use. Apparently that's quite the challenge for me it really shouldn't be. Regardless, the output prints Players.NexusCoil.Backpack.Apple.Eat:25: attempt to index upvalue 'p' (a nil value) Which I assume means it doesn't find the player? This is in a normal script just inside of the tool. Here is the code although the error seems to be connected to lines 2 and well 25.

local Tool = script.Parent;
local p = game.Players:GetPlayerFromCharacter(Tool.Parent)

enabled = true




function onActivated()
	if not enabled  then
		return
	end

	enabled = false
	Tool.GripForward = Vector3.new(-0.97, 1.02e-005, -0.243)
	Tool.GripPos = Vector3.new(-0.2, 0, -1.23)
	Tool.GripRight = Vector3.new(0.197, 0.581, -0.79)
	Tool.GripUp = Vector3.new(-0.141, 0.814, 0.563)


	Tool.Handle.EatSound:Play()

	wait(.8)
	
	local h = p.PlayerGui.HandT:FindFirstChild("Hunger")
	if (h ~= nil) then
		if (h.Value + 30 > 100) then
			h.Value = h.Value + 30
			Tool:Destroy()
		else	
			h.Value = 100
			Tool:Destroy()
		end
	end

	Tool.GripForward = Vector3.new(-1, 0, -0)
	Tool.GripPos = Vector3.new(0.2, 0, 0)
	Tool.GripRight = Vector3.new(0,0, -1)
	Tool.GripUp = Vector3.new(0,1,0)


	enabled = true
Tool:Destroy()
end


script.Parent.Activated:connect(onActivated)

1 answer

The problem is likely that tools start in the player's backpack, meaning that Tool.Parent is the Backpack, not the character. If the script is a LocalScript (which it generally should be) then you can do this:

local p = game.Players.LocalPlayer

This discussion is preserved as it was. New answers, comments, and votes are closed.