How come my npc keeps jumping?

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)
1 Like

you should increase the maximum rate so the npc doesn’t keep jumping,

(1,5) or (1,10) I recommend changing the (min, max) of the math.random

you should also add a debounce(cooldown) for the jump

3 Likes

hey Vergirl,
thanks a million for your help mate.

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)
1 Like

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

2 Likes

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
2 Likes

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.

2 Likes

awesome sauce! glad i could help.
personally i wouldnt use presim. just check if the player moved with movedirection.magnitude or so.

2 Likes

absolutely perfect mate,

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!!! :slight_smile:

1 Like

task.wait is just a better approach to wait because waitt hrottles with heartbeat or something like that.
it just yields a script.

however, task.spawn offloads the thread and makes sure it won’t yield your script.

2 Likes

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!!

1 Like

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.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.