Ad
Log in to vote
0

Player touches part, but it triggers multiple times?

Asked by 4 months ago

I have it so when a player touches a part it triggers an animation, but it triggers multiple times. Ive solved this before I just dont remember how I did it, is there a wiki page or anything on this?

Heres the script, its under starterplayer. Because thats where all my scripts go that are vital for the player.

local trigger = game.Workspace.AnimationRunners.AnimationRunner1
local player = game.Players.LocalPlayer
local cutscene1 = trigger.Cutscene1

repeat
    wait()
until player.Character

trigger.Touched:connect(function()
    local animation1 = player.Character.Humanoid:LoadAnimation(cutscene1)
    animation1:Play()   
end)

I only want it to trigger one time for forever... Dun dun daaaaah (unless they rejoin)

2 answers

Log in to vote
1
Answered by 4 months ago

Try this.

local trigger = game.Workspace.AnimationRunners.AnimationRunner1
local player = game.Players.LocalPlayer
local cutscene1 = trigger.Cutscene1
local PlayedAlready = nil

repeat
    wait()
until player.Character

trigger.Touched:connect(function()
    if not PlayedAlready then
        local animation1 = player.Character.Humanoid:LoadAnimation(cutscene1)
        animation1:Play()   
        PlayedAlready = true
    end
end)

This is referred to as debounce or when you create a variable to prevent something from cycling through multiple times when unwanted. Whenever the player activates it the first time it changes the variable "PlayedAlready" to true, by doing so if touched again it will not run as PlayedAlready is no longer nil or false.

0
If I'm correct, the script will reset itself on player reset. Or is this just in PlayerGui? ZeExplosion 205 — 4mo
0
Its fine if it resets on death. I am creating a full story mode game with AI and voices and everything so if you die the cutscene will reset. But it works so thank you :) GottaHaveAFunTime 122 — 4mo
Ad
Log in to vote
0
Answered by 4 months ago

Just add a variable, for example, local used = false. Then just make the touch check if it is false and then change it to true. However, once per game would mean creating a bool value within the player on join and doing the same as with the variable. If you need more clarifications, don't hesitate to ask.

0
So under the touched event id say "local used = false" then "if used = false then end"? GottaHaveAFunTime 122 — 4mo
0
The local used would go up with cutscene (before the event), whilst in the event you'd have 'if used == false then' 'used = true', the animation player and then 'end'. ZeExplosion 205 — 4mo

Answer this question



Ad