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