Touch events delayed in game, not studio

For some reason, my touch functions work really good in studio, for example i have a knife throw thing with align poisition that smoothly does touch events with no delay, but when i switch to the roblox game, this is no longer like i want it, a lot of my touch functions get delayed like 0.1-0.5 seconds before eventually working (The memory beetwen studio and game are the same). I dont know what is causing this, i have tried a lot of fixes but its all the same thing, maybe its lag, but if someone has some tips to fix this let me know (i have seen games with similar objectives and no delay for when i try it)

script.Parent.OnServerEvent:Connect(function(Plr, Angle, throwDirection)
if Hum.Parent:FindFirstChildOfClass(“Tool”) and Hum.Parent:FindFirstChildOfClass(“Tool”):FindFirstChild(“Throwable”) then
CurrentKnife = Hum.Parent:FindFirstChildOfClass(“Tool”)
elseif Player.Backpack:FindFirstChildOfClass(“Tool”) and Player.Backpack:FindFirstChildOfClass(“Tool”):FindFirstChild(“Throwable”) then
CurrentKnife = Player.Backpack:FindFirstChildOfClass(“Tool”)
end
local Character = Plr.Character
if Return then
Return = false
end
game.ReplicatedStorage.ClientServer:FireClient(Plr, “P”)
CurrentKnife.Handle.Decal.Transparency = 1
Plr.PlayerGui[“Custom Hotbar”].Enabled = false
CurrentKnife.Enabled = false
local humanoidRootPart = Character:FindFirstChild(“HumanoidRootPart”)
local leftHand = Character:FindFirstChild(“LeftHand”)
local throwingHandle = CurrentKnife.Handle:Clone()
local bodyVelocity = Instance.new(‘BodyVelocity’, throwingHandle)
bodyVelocity.Name = “HandleVelocity”
bodyVelocity.Velocity = throwDirection

throwingHandle.Parent = game.Workspace
throwingHandle.Name = Plr.Name.. "Handle"
throwingHandle.CanTouch = false
throwingHandle.CanCollide = false
throwingHandle.CollisionGroup = "A"

for i, Scripts in throwingHandle:GetChildren() do
	if Scripts:IsA("Script") then
		Scripts.Enabled = false
		Scripts:Destroy()
	end
end
throwingHandle.Decal.Color3 = CurrentKnife.Handle.Decal.Color3
throwingHandle.Position = Angle
local spin = Instance.new('BodyAngularVelocity', throwingHandle)
spin.AngularVelocity = Vector3.new(0, -100, 0)

throwingHandle.Decal.Transparency = 0
throwingHandle.CFrame = CFrame.new(throwingHandle.Position, throwingHandle.Position + bodyVelocity.Velocity) * CFrame.Angles(0, 0, math.rad(-90))

game:GetService("Debris"):AddItem(throwingHandle, 60)

local Hitbox = game.ReplicatedStorage.HandleBlock:Clone()
Hitbox.Parent = throwingHandle
Hitbox.CFrame = throwingHandle.CFrame
Hitbox.Name = "ThrowHibox"
Hitbox.CollisionGroup = "A"

Hitbox.CanCollide = true

Hitbox.AlignPosition.Attachment0 = throwingHandle.Bottom
Hitbox.AlignPosition.Attachment1 = Character.LeftHand.LeftWristRigAttachment
local Weld = Instance.new("Weld")
Weld.Part0 = throwingHandle
Weld.Part1 = Hitbox
Weld.Parent = Hitbox
throwingHandle.SlashTrail.Enabled = true
local Snd = CurrentKnife:FindFirstChildOfClass("Script").THROW:Clone()
if Snd then
	Snd.Parent = Plr.Character.Head
	Snd.PlayOnRemove = true
	Snd:Destroy()
end
local Snd2 = CurrentKnife:FindFirstChildOfClass("Script").Swing3:Clone()
if Snd2 then
	Snd2.Parent = throwingHandle
	Snd2:Play()
end
local BloodEmiter = script.Parent.KnifeEmitter:Clone()
BloodEmiter.Parent = Plr.Character.LeftHand
BloodEmiter.Name = "HandleE"
BloodEmiter.Enabled = false
local KnifeEmitter = script.Parent.KnifeEmitter:Clone()
KnifeEmitter.Parent = throwingHandle
KnifeEmitter.Enabled = false
local function ThrowHandler(Hit, Object)
	TouchHandle(Hit, true)
	if Return and Object == true then
		if Hit:IsDescendantOf(Plr.Character)  then
			if Hit.Parent ~= Plr.Character then return end
			if Hit:FindFirstChild("IgnoreThrowKnife") then return end
			if Hit:FindFirstChild("Hitbox") then return end
			if Hit.Name == "HumanoidRootPart" then return end

			if CurrentKnife.Configurations.LeftHanded.Value then
				if Character.RightHand:FindFirstChild("RightGrip") then
					local grip = Character.RightHand:FindFirstChild("RightGrip")
					grip.Part0 = Character.LeftHand
					grip.Part1 = CurrentKnife.Handle
					grip.Name = "LeftGrip"
					grip.Parent = Character.LeftHand
				end
			end
			
			if BloodEmiter then
				BloodEmiter:Destroy()
			end

			if KnifeEmitter then
				KnifeEmitter:Destroy()
			end
			
			if Finishing then
				throwingHandle.Name = Plr.Name.. "FinisherHandle"
				return
			else
				for i, Sounds in throwingHandle:GetDescendants() do
					if Sounds:IsA("Sound") then
						Sounds.PlayOnRemove = false
						Sounds:Stop()
						Sounds:Destroy()
					end
				end
				throwingHandle.Decal.Transparency = 1
				throwingHandle.Parent = game.ReplicatedStorage
			end

			if CurrentKnife.Parent == Player.Backpack then
				Player.Character:FindFirstChildOfClass("Humanoid"):EquipTool(CurrentKnife)
			end
			CurrentKnife.Handle.Transparency = 0
			CurrentKnife.Handle.Transparency = 1
			CurrentKnife.Handle.UnEquip:Play()
			
			local Sond = script["Grab Sound"]:Clone()
			Sond.Parent = Character.Head
			Sond.PlayOnRemove = true
			Sond:Destroy()
			
			CurrentKnife.Handle.Equip:Play()
			CurrentKnife.Handle.SlashTrail.Enabled = true
			CurrentKnife.Enabled = true
			Grab:Play()
			
			Plr.PlayerGui["Custom Hotbar"].Enabled = true
			CurrentKnife.Handle.Decal.Transparency = 0

			Idle2.Looped = false
			Idle2:Stop()

			Hitbox.CanTouch = false
			Hitbox.CanTouch = true
			
			wait(0.2)
			CurrentKnife.Handle.SlashTrail.Enabled = false
			Grab:Stop()
			Idle2:Stop()
			
			wait(0.2)
			if not Finishing then
				throwingHandle:Destroy()
			end
		end
	end
end

Hitbox.Touched:Connect(function(Hit)
	ThrowHandler(Hit, true)
end)

task.wait(0.4)
Hitbox.AlignPosition.Enabled = true
task.wait(0.1)

if Finishing then
	return
end

Return = true
if Player.Character.LeftHand.CanTouch == false then
	Player.Character.LeftHand.CanTouch = true
end
if Plr.Character.LeftUpperArm.CanTouch == false then
	Plr.Character.LeftUpperArm.CanTouch = true
end
if Plr.Character.LeftLowerArm.CanTouch == false then
	Plr.Character.LeftLowerArm.CanTouch = true
end

Hitbox.CanTouch = false
Hitbox.CanTouch = true

Snd2.PlaybackSpeed = Snd2.PlaybackSpeed - 0.01

if throwingHandle:FindFirstChild("SwordLunge") then
	throwingHandle.SwordLunge:Play()
	throwingHandle.SwordLunge.TimePosition = 1
end

local Lunge2 = script.SwordLunge:Clone()
Lunge2.PlayOnRemove = true
Lunge2.TimePosition = 1
Lunge2.Parent = Plr.Character.LeftHand
Lunge2:Destroy()
throwingHandle.SlashTrail.Color = ColorSequence.new(Color3.new(255, 0, 0))

local Rope = script.Beam:Clone()
Rope.Parent = throwingHandle
Rope.Attachment1 = Plr.Character.LeftHand.LeftGripAttachment
Rope.Attachment0 = throwingHandle.Bottom
Rope.Enabled = true
BloodEmiter.Enabled = true
KnifeEmitter.Enabled = true
Idle2:Play()
Idle2.Looped = true

end)

1 Like

In Studio your computer is running the Client and Server at the same time while testing.
When playing the game there is a ping delay between the server registering a touched event or the client registering it.
It’s one of the reasons fast moving items like bullets don’t do well with Touched events because along with other reasons lag can allow the bullet to pass through the target.

This isn’t really a Creations Feedback forum category. It’s more likely a Scripting Support forum topic which you can edit in your post.

If you copy/paste your Touched event script here you may get some help with it.

4 Likes

i edited the post , but its really long it shows weird