#Mini-Game doesn't sync anymore

1 messages · Page 1 of 1 (latest)

solid violet
#

so, here i have a mini-game with an npc that teleports in a random gift and rises from it and the player has to click him at the right moment or else he fails. the problem is as you can see, the script gets out of hand and the npc starts teleporting faster (i think the startgame() function is getting called mutliple times). here's the code:

#

Hope i'm being clear enough

sour plover
#

yup you were right

it calls itself multiple times
so just add a check

local gameRunning = false

local function startgame()
    if gameRunning then return end -- this prevent multiple instances which fixs the issue
    gameRunning = true
    
    -- ... rest of yor code …
    
    -- and now at the end, before calling startgame() again:
    gameRunning = false
    startgame()
end
solid violet
#

okay let me check

#

uh

#

@sour plover

#

@sour plover

#

@sour plover Hello ?

sour plover
#

Wsp

#

Sorry i wasn’t here

solid violet
#

np

solid violet
#

@sour plover

sour plover
#

hm

#

yeah i think i know the issue

solid violet
#

alright, what is it ?

sour plover
#

we need to revert my changes
logically it made sense but in practice it turns out to be recursive and doesn’t work as intended but i know why

revert to original script and change this:

prompt.MouseClick:Connect(function(player)
clicked = true
-- … rest of it
startgame()
end)

#

with this:

#

prompt.MouseClick:Connect(function(player)
    if stop or clicked then return end --this prevents multiple clicks so it should prevent double calls
    clicked = true
    -- … rest of it
    startgame()
end)
#

and

#

this one if clicked == false then

#

to

#

if not clicked and not stop then
#

it should fix it by avoiding double calls Mb

solid violet
#

okay let me try

#

....

#

oh my god...

#

why it doesn't work ???

#

ugh this is so annoying

sour plover
#

explain what went wrong

solid violet
#

chuckles still teleports multiple times to multiple gifts this time

#

instead of one

sour plover
#

alr wait
i’ll write a more hardcore version related to your system give me a min

solid violet
#

ok

sour plover
#

alr so

#

revert back

#

💔

#

this is baffling

solid violet
#

?

sour plover
#

its 11pm and so maybe that’s why i can’t make it work

#

Revert back to yor original version

#

at the top of your script, before any functions:

local gameInProgress = false
solid violet
#

ok

sour plover
#

local function startgame()
    -- guard: if we're already in here, do nothing
    if gameInProgress then return end
    gameInProgress = true

    -- check for end condition
    if success == requiredSuccesses or fails == requiredFails then
        stop = true
        endgame()
        gameInProgress = false
        return
    end

    clicked = false

    -- your already exising spawn logic…
    local chosengift = gifts[math.random(1, #gifts)]
    -- … etc …

    -- when the player clicks:
    prompt.MouseClick:Connect(function(player)
        clicked = true
        -- your already existing success logic…
        prompt:Destroy()
        task.wait(0.5)
        -- clear our guard *before* recursing
        gameInProgress = false
        startgame()
    end)

    -- after 2 seconds, if they never clicked:
    task.wait(2)
    if not clicked then
        fails += 1
        -- your alr existing fail logic…
        prompt:Destroy()
        task.wait(1)
        -- clear our guard *before* recursing (again)
        gameInProgress = false
        startgame()
    end

    -- no need to clear gameInProgress herre 
end
#

just replace what i added you ger it

then tell me if it fixes it

solid violet
#

ok

#

@sour plover

#

i'm about to give up.

#

too much confusion

#

i don't know..why

#

it doesn't work

#

wait wait wait

#

i think i know why

#

let me check

#

well uh, its working

#

because of your help and because i saw that i inserted chuckles's script inside another character

#

thanks for the help