Help with my taser

Currently working on a taser.
Down at line 41 to 46, I have a loop every 1 tick so that they cant jump as soon as they get shot. The issue I found was that it kept looping no matter what, I probably should have seen this coming. how would I get it so that after a second or 2 it stops looping?
(scroll down I added comments for line 43 and 46)

local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")
local ammoCount = script.Parent:WaitForChild("AmmoCount")
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local updateAmmo = ReplicatedStorage.UpdateAmmo

ammoCount.Value = 1

remote.OnServerEvent:Connect(function(player, position)
		print("Taser fired")
			local origin = shoot_part.Position
			local direction = (position - origin).Unit*250
			local result = Workspace:Raycast(origin, direction)
			if ammoCount.Value < 1 then
				ammoCount.Value = 1
				wait(2)
			else
			ammoCount.Value -= 1
			print("ammo used")
				local intersection = result and result.Position or origin + direction
				local distance = (origin - intersection).Magnitude

				local bullet_clone = ReplicatedStorage.Bullet:Clone()
				bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
				bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
				bullet_clone.Parent = Workspace
				bullet_clone.Anchored = true
				bullet_clone.CanCollide = false

			if result then
				print("if result")
				local part = result.Instance
				local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
				
				local function sit()
					humanoid.Sit = true
				end
				
			if humanoid then
				print("humanoid tasered")
				while wait(1) do -- line 43
					sit()
					print("looping the sit")
				end -- line 46
				print("Destroying bullet clone")
				wait(1)
				bullet_clone:Destroy()
				print("destroyed bullet clone")
			else
				print("Destroying bullet clone2")
				wait(1)
				bullet_clone:Destroy()
				print("destroyed bullet clone2")
			end
		end
	end
end)

Try something like this here:

image

print("humanoid tasered")

local sit_duration_in_seconds = 3  -- if possible, put this in the top of the script
local must_seat_until = tick() + sit_duration_in_seconds

while must_seat_until < tick() do
   sit()
   wait()
end
1 Like

It looks like it would work, but it leads to one interesting bug, any idea how to fix? robloxapp-20210227-1318454.wmv (3.0 MB) sorry for low quality vid

1 Like

I may be wrong, but it’s probably because of the sit method. Can’t you ragdoll the player?

1 Like

I probably could but I have no clue how to make a ragdoll script without killing the player to cause the ragdoll

1 Like

Take a look at this topic, it might help :slight_smile:

1 Like

alright, ill have a look, thanks for the help!

1 Like