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:
#Mini-Game doesn't sync anymore
1 messages · Page 1 of 1 (latest)
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
okay let me check
uh
@sour plover
there's another problem now..
@sour plover
@sour plover Hello ?
np
alright, what is it ?
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
okay let me try
....
oh my god...
why it doesn't work ???
ugh this is so annoying
explain what went wrong
alr wait
i’ll write a more hardcore version related to your system give me a min
ok
?
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
ok
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
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