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

How do i make a touch script activate only once and never again for the same player?

Asked by
Nogalo 148
7 years ago
Edited 7 years ago

I'm trying to make a script which i would put inside trophies around the map and display how many trophies each player has however after a whole afternoon of brainstorming i got nil

script.Parent.Touched:connect(function(hit)
    if hit.Parent ~= nil then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player ~= nil then
            local stats = player:WaitForChild("leaderstats")
            local trophies = stats: WaitForChild("TrophiesFound")
                local humanoid = hit.Parent:WaitForChild("Humanoid")
                if humanoid ~= nil then
                    if humanoid.Health ~= 0 then
                        trophies.Value = player.leaderstats.TrophiesFound.Value + 1

                    end
                end
            end
        end
    end)

All i've got is this script that adds +1 on the list of the player who touched it but it adds 5 every time i move near it or walking over it. If you can give me any kind of tip or suggestion your input would be greatly appricated

0
Fix your code block, please. Goulstem 8144 — 7y

2 answers

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

I won't give you actual code, but I'll let you fill in the blanks.

You can get a player by their character from game:GetService("Players"):GetPlayerFromCharacter. From there, you can put that player into a table, and later iterate over the table and end the function if they're in it.

Ad
Log in to vote
-1
Answered by 7 years ago
Edited 7 years ago

Put a Boolean value in StarterGui and have the function change it to true if the player clicks it. Have an if statement check whether or not it is true.


script.Parent.Touched:connect(function(hit)

if (hit.Parent ~= nil) then

    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    if (player ~= nil) then

        if (player.PlayerGui.ClickedValue.Value == false) then --Check if false

            local stats = player:WaitForChild("leaderstats")

            local trophies = stats:WaitForChild("TrophiesFound")

            local humanoid = hit.Parent:WaitForChild("Humanoid")

            if (humanoid ~= nil) then

                if (humanoid.Health > 0) then   -- ~= was changed to >

                    trophies.Value = player.leaderstats.TrophiesFound.Value + 1

                    player.PlayerGui.ClickedValue.Value = true --Set to true

                end
            end
        end
    end
end

end)

Also, you might want to consider cleaning up your code. Space out lines and put loop conditions in parentheses.

0
So, who is the moron that downvoted this? KillPowerAlex135 5 — 7y
0
After some tinkering i've gotten your script to work however it only works once. After the first trophy the boolean is permenantly true and it doesn't work again Nogalo 148 — 7y
0
Is that not what you wanted? KillPowerAlex135 5 — 7y

Answer this question