Ad
Log in to vote
0

Player touches part, but it triggers multiple times?

Asked by 9 days 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)

Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

2 answers

Log in to vote
1
Answered by 9 days 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 11 — 9d
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 27 — 9d
Ad
Log in to vote
0
Answered by 9 days 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 27 — 9d
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 11 — 9d

Answer this question



Ad