Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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
0
Thanks for the answer, but can you be a bit more specific about where I would use a remote event? Howatcha 5 — 3y

2 answers

Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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.

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' " Howatcha 5 — 3y
1
Replace FindFirstChild("Humanoid") with WaitForChild("Humanoid"). If 0 instances can be found with the name "Humanoid" using FindFirstChild, it will return nil. appxritixn 2235 — 3y
Ad
Log in to vote
0
Answered by
Rukreep 15
3 years ago

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

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? Howatcha 5 — 3y
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. Rukreep 15 — 3y

Answer this question