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

Is it possible to reference a player by their username/userID?

Asked by 4 years ago

So I want to make a game where if the player clicks on a button, their character dies. The only problem is, whenever I try to reference the player using "Player", the console says that "Player is not a valid member of workspace", and when I tried referencing Players instead of workspace, I pretty much got the same error.

I'm new to scripting so I decided to borrow some code and modify it, this is the code:

local script:

local ClickDetector = game.Workspace.Button.ClickDetector --Sets variable for clickdetector

ClickDetector.MouseClick:Connect(function() -- declares function for click event
    game.Workspace.ClickEvent:FireServer() -- fires server
end)

serverscript:

game.Workspace.ClickEvent.OnServerEvent:Connect(function()

end)

the serverscript is empty because I couldn't figure out how to reference the player and set their health to 0.

Any help would be appreciated.

0
Try looking stuff up in the API Reference before you ask, It's a very helpful resource https://developer.roblox.com/en-us/api-reference/ Filipalla 504 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

ClickDetector won't need RemoteEvent, so every thing you need is just a Server Script and ClickDetector.

ClickDetector.MouseClick event has Player instance(the Player that click the detector), so the script would be simple:
Script:

local ClickDetector = game.Workspace.Button.ClickDetector

ClickDetector.MouseClick:Connect(function(player) -- set player parameter(the player click it)
    --kill player
end)

My final advice is:
-Don't put RemoteEvent in Workspace, it better to put it in ReplicatedStorage
-Use Players.LocalPlayer to get LocalPlayer(only work for LocalScript)

Some documents:
ClickDetector

RemoteEvent

Players.LocalPlayer

Ad

Answer this question