i have a randomNumber variable that chooses a random number between 1 and 2 and if it chooses 1 then i want my npc to jump once and if it chooses 1 again then it will do the same thing but if 2 is chosen, he won’t jump.
i assume my npc is jumping because of the presimulation event running every frame and what not but idk how to solve this. i appreciate any help ty!!
local somedudethatfollowsu:Model = workspace.somedudethatfollowsu
local somedudethatfollowsuHumanoid:Humanoid = somedudethatfollowsu.Humanoid
local pfs = game:GetService("PathfindingService")
local RS = game:GetService("RunService")
local startingPosition = somedudethatfollowsu:FindFirstChild("HumanoidRootPart").Position
local endingPosition = workspace.SpawnLocation.Position
game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(c)
RS.PreSimulation:Connect(function()
local randomNumber = math.random(1,2)
somedudethatfollowsuHumanoid:MoveTo(c.HumanoidRootPart.Position)
if randomNumber == 1 then
somedudethatfollowsuHumanoid.Jump = true
end
end)
end)
end)
what i did was; i added a canJumpDB debounce variable but i’m not very good with debounce patterns, i tried this but i can’t seem to get it to work.
local canJumpDB = false
game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(c)
connection = RS.Stepped:Connect(function()
local randomNumber = math.random(1,10)
local mag = (somedudethatfollowsu.HumanoidRootPart.Position - c.HumanoidRootPart.Position).Magnitude
somedudethatfollowsuHumanoid:MoveTo(c.HumanoidRootPart.Position)
if randomNumber == 1 then
if not canJumpDB then
canJumpDB = true
somedudethatfollowsuHumanoid.Jump = true
canJumpDB = false
end
end
end)
end)
end)
what your doing is SPAMMING it, so your not getting insanely luck sad.
presim is before physics simulation like EVERY frame.
so your basically rolling a bias dice because your not only saying move here.
if random 1 2 will always end up 1 if that makes sense
and adding on, your jumpdb is also doing the same.
on off instantly.
add a wait() but preferably task.wait and task.spawn(fucntion() to make it not yield
if randomNumber == 1 then
if not canJumpDB then
canJumpDB = true
somedudethatfollowsuHumanoid.Jump = true
task.spawn(function()
task.wait(0.5) --// placehodler
canJumpDB = false
end)
end
end
oh yes that does make a lot more sense. i knew it had to do with the event tbf but i’m only using it just so my npc will update position based on if i move too; which works fine.
thank u so much for ur help honestly. from what i’m gathering the task.wait acts as a cooldown before choosing a random number again and therefore making the NPC jump afterwards.
i honestly wouldnt have thought of this in 100 years cheers!!!
u helped a ton !
i’ll switch the event around to something else, i used presim because i didn’t know what to choose from the options in runservice. many had missing explanations but i assume they’re in development. i’ll make sure to keep that in mind as well. thanks a million anxiety!!
you’re right. task.wait performance wise is superior, faster to respond as well. i’ve seen task.spawn a while ago but never used it until now so thanks for the info.