Well for this purpose, you can use a handy function of the Players service called GetPlayerFromCharacter().
This function, like this name suggests, it returns a player object when given a character value. This can be used as the first argument of the FireClient() function of the remote event.
Another thing to note is that lua is case sensitive, meaning that something like end has a different meaning than something like End, eNd, or END. It should always be clear that capitalization in the correct places matter, for example: if you tried to write an if statement like:
If x Then... end
It would error and neither If nor Then are written correctly, however, if I did
if x then ... end
it wouldn't error as if and then are correctly formatted keyword
With that said, the following can be done:
local GuiPop = game.ReplicatedStorage:WaitForChild(“GuiPopUp”)
workspace.TestPad.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
GuiPop:FireClient(plr)
end
end)
Now, there is a slight problem with the code above,that being that it fires multiple times, as a character has multiple bodyparts can could be colliding with the part within a fraction of a second. That number being from 6 for R6 characters to 16 for R15 character.
An easy way to avert this would be to use a debounce.
Ex:
local GuiPop = game.ReplicatedStorage:WaitForChild(“GuiPopUp”)
local PoppedPlayers = {}
workspace.TestPad.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and not PoppedPlayers[plr] then
GuiPop:FireClient(plr)
PoppedPlayers[plr] = true
wait(1)
PoppedPlayers[plr] = nil
end
end)
Hopefully this helped you out!
2 comments
The Touched event doesn't run a large amount of times because of multiple body parts. It runs that many times due to collisions happening multiple times per second.
Thank you so much man, it worked!