Im using evaera’s Promise modulescript for “waiting” for a fish to catch in my fishing rod script, im getting the error of “Unhandles Promise rejection” and i believe its coming from me not actually being able to cancel the promises
Ive tried doing this:
function WaitForFish(Time,Player)
if Time ~= 0 then
local waitPromise = Promise.delay(Time):andThen(function()
--body
end)
if Time == 0 then
waitPromise:cancel()
end
end
end
and calling WaitForFish(0,player) to cancel the promise, but the if time = 0 is inside the if it ~= 0, so it doesnt work. If i take the if time = 0 outside the if ~= 0, like this:
function WaitForFish(Time,Player)
if Time ~= 0 then
local waitPromise = Promise.delay(Time):andThen(function()
--body
end)
end
if Time == 0 then
waitPromise:cancel()
end
end
It also doesnt work due to waitPromise not being a global variable, it is required to be a global variable because otherwise other players are able to cancel other players promises through regular gameplay.
Is there something in the promise module im missing? to make this easier? I keep trying a bunch of stuff but nothing is seeming to work, thanks for any help.
It’s erroring because after the delay of the promise theres a line that refers to something that would be in workspace if the player didnt cancel it, but when i try and make the promise cancel its not working and still trying to look for the thing in workspace
It is because waitPromise is not defined. You must define the waitPromise variable before the condition, like that:
function WaitForFish(Time,Player)
local waitPromise = nil
if Time ~= 0 then
waitPromise = Promise.delay(Time):andThen(function()
--body
end)
end
if Time == 0 and waitPromise ~= nil then
waitPromise:cancel()
end
end
In lua, you cannot use a variable that you did not declare before (You cannot use a declared variable which is not declared in the same chain as the line) like for this script:
local test = 10
print(test)
test is declared in the same chain so it will return 10 in the output but if it is
if true then -- your condition
local test = 10
print(test) -- return 10 because test is declared in the same chain
end
print(test) -- return nil because test is not declared in the same chain
To bypass this problem, you should declare the variable before the condition like that:
local test -- you can also use "local test = nil", it is the same thing
if true then -- your condition
test = 10
end
print(test) -- if your condition is valid then it will print 10 because we defined test to 10 but if your condition is not valid then it will print nil because test has been defined to nil