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

i need to know how to refer a specific player when he touches something ?

Asked by 3 years ago

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

2 answers

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

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)
0
thank you so much this is exactly what i need thank you so much typhoon1421 22 — 3y
Ad
Log in to vote
1
Answered by 3 years ago

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
0
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* BreckleShrimp 23 — 3y
0
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 typhoon1421 22 — 3y

Answer this question