Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

when the npc attacks it slows down but never speeds up again, any clue why?

there are no error code, it just doesn't seem to work


local attackCooldown = 1 
local attackWindup = 0.5
local attackRange = 3
local attackDamage = 20

local char = script.Parent.Parent
local hum = char:WaitForChild("Humanoid")
local originalWalkSpeed = hum.WalkSpeed

local canAttack = true

script.Parent.AttackRemote.Event:Connect(function(plrRoot)

	if not canAttack then return end
	canAttack = false
	
	hum.WalkSpeed = 0.1

	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://7215836748"
	local playAnim = hum:LoadAnimation(anim)
	playAnim:Play()

	wait(attackWindup)
	local newDistance = (char.HumanoidRootPart.Position - -plrRoot.Position).magnitude
	if newDistance <= attackRange + 5 then 
		
	if plrRoot.parent.Player.isBlocking.Value == true then
		attackDamage *= 0.5
	end
	plrRoot.Parent.Humanoid:takeDamage(attackDamage)

	wait(attackCooldown)
	hum.WalkSpeed = originalWalkSpeed
	canAttack = true
	
	end
	end)

2 comments

0

fixed codeblock

0

did you use print statements?

1 answer

I think the NCP might be breaking because you defined origin walkspeed as the humanoid's walkspeed. This means when you use originalWalkSpeed, it gets the humanoid's current walkspeed. You're basically telling the script to set the humanoid's walkspeed to itself. You can fix this by writing something similar to this:

local OriginWalk = instance.new("NumberValue", char)
OriginWalk.Value = hum.Walkspeed

You can then use this value later in the script, rather than originalWalkSpeed.

12 comments

0

do i want to erase any line or just put those lines at the end?

0

eras local originalWalkSpeed = hum.WalkSpeed and put that script at that line

0

No, you replace where you define originalwalkspeed with the two lines, then erase originalwalkspeed on line 34 and replace it with OriginWalk.

0

it came out with an error Workspace.Entity.Controller.Attack:8: attempt to index nil with 'new'

0

Oh, my bad. The instance's "I" should be capitalized

0

that didnt fix it

0

hes still slow i mean, it did fix the other error tho.

0

Where did you write the two lines of code? If you haven't already, try putting the code where you're defining everything.

0

i replaced line 8 with local OriginWalk = Instance.new("NumberValue", char) and add a line underneath for OriginWalk.Value = hum.WalkSpeed 

0

Okay, try this. Manually add a value in the ncp and replace "Instance.new("NumberValue", char)" with script.Parent.WhateverYouCalledTheValue

0

local sprint = ?

0

So it's possible the code isn't speeding up because the value, as I said in the answer, is pointing to the walkspeed. You can fix this by adding a value into the humanoid manually and getting the value from the script.

This discussion is preserved as it was. New answers, comments, and votes are closed.