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

Why will this code not work? Error is: "attempt to index local 'Plr' (a nil value)".

local EnergyBar = script.Parent

function getPlayerName(Plr)

return Plr.Name

end

game.ReplicatedStorage.PlayerName.OnServerEvent:Connect(getPlayerName())

EnergyBar.Activated:Connect(function()

print("test")

local player = getPlayerName()

print(player)

local character = game.Workspace[player]

player.Character.Humanoid.Health = player.Character.Humanoid.Health + 100

player.Character.Humanoid.WalkSpeed = 21

wait(30)

player.Character.Humanoid.WalkSpeed = 16

end)

1 answer

Ah, it is a simple mistake with your argument. Because you didn't pass a parameter to the Plr argument, the parameter to the argument is nil. Just define it like this or some other way:

local function getPlayerName(Plr)
	return Plr.Name
end
game.Players.PlayerAdded:Connect(getPlayerName)

The above script will work because PlayerAdded passes the player who joined the game as its parameter. This means that Plr will have that parameter passed to it.

2 comments

0

I changed this line of code but it still does not work. "game.ReplicatedStorage.PlayerName.OnServerEvent:Connect(getPlayerName)"

0

The FireServer() end of the RemoteEvent must be triggered from an event that passes a player. This includes PlayerAdded.

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