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

How can i get the player from the character, i have tried something but it displays an error?

This is the script:

wait()




function onTouched(part)

	local h = part.Parent:FindFirstChild("Humanoid")
	

	local character = h.Parent
local player = game.Players:GetPlayerFromCharacter(character)

if player then
    print("Player is " .. player.Name)
else
    print("Player doesn't exist!")

local dmg = player.PlayerStats.Melee.Value
	

	if h~=nil then
		
		local anim = h:LoadAnimation(script.anim)
		
		h:TakeDamage(dmg)
		
		anim:Play()
		
		end
	end

	wait(1)
end



script.Parent.Touched:connect(onTouched)

So i need to get the player from the character so i can get the player's melee stat and use it as the damage value, but this script results in an error, this is what my output displayed:

Workspace.iiiAmericanLight.RightHand.Dmg.Script:20: attempt to index local 'player' (a nil value)

1 comment

0

Why are you using player when you know it does not exist???

1 answer

Move lines 19-30 to after line 15.

Right now you're making sure that there is no player, and then trying to do stuff with the player. Basically put the code on the wrong side of the if/else.

You can also remove the h declaration and use player = game.Players:GetPlayerFromCharacter(part.Parent)

3 comments

0

That does technically fix all the errors, but what i need to do is get the player, and it's not happening, do u have any solutions to that?

0

It's not printing Player is soandso? Edited answer too

0

Still nothing.

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