Need help with ability delay

Basically, when I use an ability that throws a sword, the sword goes in the direction from where I was looking when I first activated the ability.
Look → activate → look somewhere else → sword goes where I first looked

I’ve tried making it delayed when it stores the position, but it clearly doesn’t work.

Video vv

Currently, this is the script:

local module = {}

local runService = game:GetService("RunService")
local effectsRemote = game.ReplicatedStorage.Events.ClientEffect
local replicatedStorage = game.ReplicatedStorage
local modulesFolder = game.ServerStorage.Modules
local muchachoHitbox = require(modulesFolder["MuchachoHitbox"])
local replicatedModulesFolder = game.ReplicatedStorage.Modules
local effectHandler = require(replicatedModulesFolder.EffectHandler)
local hitModule = require(modulesFolder["Hit"])

local cooldowns = {}

local params = OverlapParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude

function module.SwordThrow(character)
	local humanoid = character:WaitForChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(character)
	local rootPart = humanoid.RootPart
	local ability1Animation = humanoid:LoadAnimation(replicatedStorage.Animations.Monster.Abilities["1x1x1x1"].Ability1)
	local abilitySfx = replicatedStorage.SFX.Monster.Abilities["1x1x1x1"].Ability1
	local swordModel = replicatedStorage.Items.Other["1x1x1x1Sword"]:Clone()
	local sfxFolder = replicatedStorage.SFX

	if cooldowns[character] or player:GetAttribute("Attacking") or player:GetAttribute("Stunned") then return end
	cooldowns[character] = true
	
	params.FilterDescendantsInstances = {character}
	
	local hitbox = muchachoHitbox.CreateHitbox()
	hitbox.CFrame = swordModel
	hitbox.Size = Vector3.new(4,4,4)
	hitbox.Visualizer = true
	hitbox.OverlapParams = params
	hitbox.Key = character.Name .. "m1Attack"
	hitbox.VelocityPrediction = true

	player:SetAttribute("Attacking", true)
	rootPart.Anchored = true
	
	effectHandler.Effects(character.Torso, abilitySfx, false)

	task.delay(2/3, function() -- Delayed to .667 seconds
		hitbox:Start()

		local bodyVelocity = Instance.new("BodyVelocity")
		bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		bodyVelocity.Parent = swordModel

		swordModel.Parent = workspace
		swordModel.CFrame = rootPart.CFrame

		bodyVelocity.Velocity = rootPart.CFrame.LookVector * 100

		task.delay(15, function() -- Destroy sword after 15 seconds
			swordModel:Destroy()
		end)
	end)

	
	hitbox.Touched:Connect(function(hit)
		local possibleCharacter = hit.Parent
		local ehum

		if player:GetAttribute("Stunned") then
			return
		else
			if (possibleCharacter:IsA("Model") and possibleCharacter:FindFirstChild("Humanoid")) then
				ehum = possibleCharacter:FindFirstChild("Humanoid")
			elseif (possibleCharacter.Parent:IsA("Model") and possibleCharacter.Parent:FindFirstChild("Humanoid")) then
				ehum = possibleCharacter.Parent:FindFirstChild("Humanoid")
			elseif (possibleCharacter.Parent.Parent:IsA("Model") and possibleCharacter.Parent.Parent:FindFirstChild("Humanoid")) then
				ehum = possibleCharacter.Parent.Parent:FindFirstChild("Humanoid")
			elseif (possibleCharacter.Parent.Parent.Parent:IsA("Model") and possibleCharacter.Parent.Parent.Parent:FindFirstChild("Humanoid")) then
				ehum = possibleCharacter.Parent.Parent.Parent:FindFirstChild("Humanoid")
			else
				return
			end
		end

		local hitSound = sfxFolder.Monster.HitSFX["BloodVariant"..tostring(math.random(1,5))]

		local hs = hitModule.Hit(ehum, 25, humanoid.RootPart.CFrame.LookVector * 5, 2, 2)

		if hs then
			effectHandler.Effects(character.Torso, hitSound, true)
		end
	end)

	ability1Animation:Play()
	task.wait(1)
	player:SetAttribute("Attacking", false)
	rootPart.Anchored = false
	task.wait(0)
	cooldowns[character] = false
end

return module

what does “Roblox only has root position once” mean? (im slow) also uh whats wrong with ts

1 Like

nvm Ignore what I said, the error is simple
it’s keep spawn bodyVelocity more then one

[when it’s keep spawn more then one bodyVelocity ]


[with one bodyVelocity ]

Here is my code

local SpawnPart = Part:Clone()
				
SpawnPart.Parent = workspace
SpawnPart.CFrame = Character.HumanoidRootPart.CFrame
				
SpawnPart.CanCollide = false
				
local bodyVelocity = Instance.new("BodyVelocity", SpawnPart)
								
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Velocity = Character.HumanoidRootPart.CFrame.LookVector * 100
								
Debris:AddItem(SpawnPart, 3)

check your sowrd model
image

1 Like

image
just 1 body velocity
also my issue wasn’t that it goes in some random direction, it was that it stores the position too fast from what it seems, but even if i store it on a delay, it still goes in the direction of where i first activated it

1 Like
swordModel.CFrame = rootPart.CFrame
bodyVelocity.Velocity = rootPart.CFrame.LookVector * 100

You are literally setting the model’s CFrame and adding the velocity to it at the same time, so there shouldn’t be any delay. It’s not as if you referenced the LookVector too early or anything like that.

I believe the issue is more likely related to the natural server delays or similar factors, and the only solution would be to make it client-side.

(wrong reply, sorry)

1 Like

He is right handling on the client side would be a better way to handle delay

This is incorrect, when a function is called, the properties that it reads are going to be the whatever they are at the time the line of code is executed.

The cause of this bug is the character movement desync between the client and server, and as you mentioned:

Anchoring a player character’s root part doesn’t prevent them from rotating it using shift lock on the client. Despite seemingly being able to rotate freely on your client, the server does not see any changes to your character as you lose network ownership of your character when it’s anchored.

If you want to stop the character from moving around, but still being able to look around, you can use a body velocity with a velocity of (0, 0, 0) and max force of (inf, 0, inf). Just destroy it after the ability ends so they can move around again.

Alright! I didn’t know that rotation while being anchored wasn’t replicated on the server.
Thank you for the informations and clarifications.

1 Like

thank you! i tried this and it worked. my code is pretty sloppy so i just slapped it in with another localscript and theres also a beam kinda tweaking in the back, but it does work

image
image

1 Like

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