Player velocity not affected by script

Hey developers! I am making an attack script, but the velocity seems to work on character models, and not player characters.

The AssemblyLinearVelocity property isn’t even affected!

Example:

The script is listed below:

Attack script
local AttackRemoteEvent = game:GetService("ReplicatedStorage").RemoteEvents.Attacks -- Gets the remote event
local Settings = require(script.Settings) -- Gets attack settings
local RagdollModule = require(script.Parent.Ragdoll)

local function PlayAnimation(humanoid, id) -- Defines animation playing function
	id = "rbxassetid://" .. tostring(id) -- Turns ID into a string
	local anim = Instance.new("Animation") -- Creates new animation
	local animator = humanoid:WaitForChild("Animator") -- Defines animator
	
	anim.AnimationId = id -- Adds the ID to the animation
	
	local animTrack = animator:LoadAnimation(anim) -- Loads the animation
	
	print("Played animation ID " .. tostring(id)) -- Prints the animation ID
	
	animTrack:Play() -- Plays the animation
	animTrack:Destroy() -- Destroys the animation track
	anim:Destroy() -- Destroys the animation
end

local function TemporaryRagdoll(char, t) -- Defines temporary function
	RagdollModule.Ragdoll(char) -- Ragdolls player
	task.wait(t) -- Waits some time
	RagdollModule.Unragdoll(char) -- Unragdolls player
end

-- Gets the client fired attack
AttackRemoteEvent.OnServerEvent:Connect(function(player, attack)
	attack = tostring(attack).lower(attack) -- Converts the attack into lowercase
	
	print(player.DisplayName .. " (@" .. player.Name .. ") requested to " .. attack) -- Prints who requested an attack
	
	local char = player.Character or player.CharacterAdded:Wait() -- Gets player character
	local torso = char:WaitForChild("Torso") -- Gets player torso
	
	if attack == "punch" then -- Checks type of attack
		PlayAnimation(char:WaitForChild("Humanoid"), 123943615017025) -- Plays the animation
		
		attackHitbox = char["Right Arm"].Touched:Connect(function(hit) -- If the arm is touched, do this
			
			if hit.Parent ~= nil and hit.Parent:FindFirstChild("Humanoid") then
				-- Define enemy parts and min / max random angular velocities, and attack window
				local enemyChar = hit.Parent -- Enemy character
				local enemyHumanoid = enemyChar:WaitForChild("Humanoid") -- Enemy humanoid
				local enemyTorso = enemyChar:WaitForChild("Torso") -- Enemy torso
				local enemyHumanoidRootPart = enemyChar:WaitForChild("HumanoidRootPart")
				local minRandom = 50 -- Minimum degree of angular rotation
				local maxRandom = 150 -- Maximum degree of angular rotation
				
				enemyHumanoid.Health -= Settings[attack].damage -- Damages enemy
				enemyHumanoidRootPart.AssemblyLinearVelocity = (torso.CFrame * CFrame.new(Settings[attack].direction * Settings[attack].intensity)).Position -- Sets enemy velocity
				enemyHumanoidRootPart.AssemblyAngularVelocity = Vector3.new(math.random(minRandom, maxRandom), math.random(minRandom, maxRandom), math.random(minRandom, maxRandom)) -- Sets enemy angular velocity
				task.spawn(TemporaryRagdoll, enemyChar, Settings[attack].ragdollTime) -- Ragdolls enemy for the amount of time
				attackHitbox:Disconnect() -- Disconnects the function when ran
			end
		end)
		
		task.wait(Settings[attack].attackWindow) -- The window to attack the enemy
		
		attackHitbox:Disconnect() -- Stops attack window
	end
end)
1 Like

This happens because you do not have the network ownership of the player’s parts, you should fire a RemoteEvent to all Clients and handle the Velocity on the Client.

1 Like
  1. Network Ownership: Ensure that the server has network ownership over the player’s character when applying physics changes. If the client owns the character, the server’s physics changes might not take effect.

    enemyHumanoidRootPart:SetNetworkOwner(nil) -- Give server ownership temporarily
    
  2. AssemblyLinearVelocity: Make sure the calculation of AssemblyLinearVelocity is correct. You are using CFrame.Position which might not be what you intend. Instead, you might want to calculate a vector direction:

    local direction = (torso.CFrame.LookVector * Settings[attack].intensity)
    enemyHumanoidRootPart.AssemblyLinearVelocity = direction
    
  3. Character Parts: Ensure that all character parts are correctly named and exist when accessed. Any missing part will cause an error and prevent the script from executing as expected.

  4. Animation Timing: The timing of animations and physics effects should be considered. Sometimes physics effects need to be synchronized with animation events.

  5. Debugging: Use print statements to debug the values of AssemblyLinearVelocity and AssemblyAngularVelocity to ensure they are being set as expected:

    print("Velocity:", enemyHumanoidRootPart.AssemblyLinearVelocity)
    print("Angular Velocity:", enemyHumanoidRootPart.AssemblyAngularVelocity)
    

Thank you ChatGPT for that nice explanation :pray: :skull::skull:

1 Like


if they use too many comments in code examples they are 99% chatgpt

he also just throws the script and leaves the topic and never come back.

1 Like

Thank you so much! I can’t believe I didn’t think of this.

1 Like

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