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

Can anyone help me with a stun script for my sword fighting game?

Hello, I need help with an upcoming sword fighting game I'm making. Right now, I am working on the combat system. It is based around a number, which we will call buildup. Buildup starts at 21 and goes down by 2 each time you get hit. When it reaches a number lower than 7, you will get stunned. Right now I am working on the main part, getting stunned. I have yet to work on the hit detection, but if you look in the script, I can change a value so that it would simulate a hit (The value would go down). Anyway, while testing the script, it did not seem to work at all. Even though there were no errors in the output. I am very VERY new to scripting and would like all the help I can get (well I'm not forcing you but, you get it). Thanks in advance.

local buildup = script.Parent
local hum = script.Parent.Parent.Parent:FindFirstChild("Humanoid")
stunned = false

if buildup.Value <= 7 then
	if stunned == false then
		stunned = true
		print("epic")
		local StunAnim = hum:LoadAnimation(script.Parent.StunAnim)
		StunAnim:Play()
		hum.WalkSpeed = 0
		if stunned == true then
			wait(5)
			stunned = false
			StunAnim:Stop()
			hum.WalkSpeed = 16
			buildup.Value = 21
		end
	end
end

1 comment

0

Thanks for the answer, but can you be a bit more specific about where I would use a remote event?

2 answers

You are only checking the value of buildup once. You require a loop or an event to continuously check what the value of buildup is.

As it appears buildup is a NumberValue or IntValue, this should work:

local buildup = script.Parent
local hum = script.Parent.Parent.Parent:WaitForChild("Humanoid") --:FindFirstChild("Humanoid")
stunned = false

buildup.Changed:Connect(function(newValue) -- Whenever buildup's value is changed, this will get called.
	if newValue <= 7 then -- 'newValue' is the value buildup was previously changed to
    		if stunned == false then
        		stunned = true
        		print("epic")
        		local StunAnim = hum:LoadAnimation(script.Parent.StunAnim)
        		StunAnim:Play()
        		hum.WalkSpeed = 0
        		if stunned == true then
           			wait(5)
            			stunned = false
            			StunAnim:Stop()
            			hum.WalkSpeed = 16
            			buildup.Value = 21
        		end
    		end
	end
end)

EDIT

If you are changing buildup's value from local scripts, it leaves you with security vulnerabilities. I would highly recommend using RemoteEvents if a local script needs to change buildup's value, and changing the value on the server instead.

2 comments

0

Sorry to bother you so much, with the script it prints "epic" but does not play the animation. There is also an error in the ouptut saying "Players.Howatcha.Backpack.Sword.BuildUp.LocalScript:14: attempt to index nil with 'LoadAnimation' "

1

Replace FindFirstChild("Humanoid") with WaitForChild("Humanoid"). If 0 instances can be found with the name "Humanoid" using FindFirstChild, it will return nil.

As far as I know you gotta use RemoteEvents. RemoteEvents send commands from the client to the server. To learn more, click here: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

2 comments

0

Thanks for the post, but can you be a bit more specific about where I would go about putting this remote event? And what could I use it for?

0

I'm not the kind of person that is the best with RemoteEvents. I just figured out that would be the thing. Sorry pal.

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