Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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.

0
I changed this line of code but it still does not work. "game.ReplicatedStorage.PlayerName.OnServerEvent:Connect(getPlayerName)" jhawkes145 0 — 5y
0
The FireServer() end of the RemoteEvent must be triggered from an event that passes a player. This includes PlayerAdded. DeceptiveCaster 3761 — 5y
Ad

Answer this question