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.
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