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

Finding player in workspace doesn't work even if it works with print?

Asked by 4 years ago

So I'm trying to make a thing where it changes a person's nametag to say something if a remote event is fired. My serverside code is as follows:

local revent = game:GetService('ReplicatedStorage').class
local Player = game.Players.LocalPlayer
local function test (Player,classes) 
if classes == 1 then
print(Player)
game.Workspace.Player.Humanoid.MaxHealth = 5--Just to test the waters.
end
end
revent.OnServerEvent:Connect(test)

When I run this, I get an error: Player is not a valid member of Workspace Please help.

1 answer

Log in to vote
1
Answered by 4 years ago
--Wrong Code:
local revent = game:GetService('ReplicatedStorage').class
local Player = game.Players.LocalPlayer --You can't define LocalPlayer in script.
local function test (Player,classes) 
if classes == 1 then
print(Player) --You can't print object. You need to use .Name or tostring().
game.Workspace.Player.Humanoid.MaxHealth = 5--When using OnServerEvent, you can use Player.Character too.
Instead of this we can say: Player.Character
end
end
revent.OnServerEvent:Connect(test)

--Fixed Code:
local revent = game:GetService('ReplicatedStorage').class
local function test (Player,classes) 
if classes == 1 then
print(Player.Name) --or print(tostring(Player))
Player.Character.Humanoid.MaxHealth = 5
end
end
revent.OnServerEvent:Connect(test)
Ad

Answer this question