i need a help that i need to i am doing a touched event that whenever a player touches a brick he get a reward so how do i refer to that particular player who touched the brick i dont want to give reward everyone in server just the person who touched the brick also i want the code to fire only once for each player like he cant just jump on brick and get more rewards but if any other person comes he will get the reward but only once
i need to know how to refer a specific player when he touches something ?
2 answers
You can use the Players:GetPlayerFromCharacter function to get the player from the Character that touched the part.
To make it so that player can only claim the reward once, you could create a table and insert the players that have touched the part, and make an if statement to check if they are in the table. And if they are, then it won't give them a reward.
Server Script inside Part.
local PlayersTouched = {}
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then -- Checking if the thing that touched the part is a player, note that this will also detect NPCs.
if not table.find(PlayersTouched, hit.Parent.Name) then -- Checking if the player is not in the table.
table.insert(PlayersTouched, hit.Parent.Name) -- Inserting the player's name into the table.
local Player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player from the Character that touched the part.
-- Code to give player the reward(s).
-- Example:
local Cash = Player:WaitForChild("leaderstats"):WaitForChild("Cash")
Cash.Value = Cash.Value + 5
else -- If they are in the table
print("Player has already claimed this reward.")
end
end
end)
To get a player from the touched event, use this:
Part.Touched:Connect(function(hit)
if hit.Parent:WaitForChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
--the rest of your code
end
end
2 comments
Also, if this is in a local script, and it is happening for everyone, you need to check to see if Local Player is equal to the player that touched the Brick. To do this, write the variable: local LocalPlayer = game.Players.LocalPlayer then write the if statement inside of the function: if player == LocalPlayer then
uh see i am making a obby and i want that whenever player touches the checkpoint he gets 50 coins and also i have aldready made my leaderstats so how will i do this
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
thank you so much this is exactly what i need thank you so much