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.
01 | local buildup = script.Parent |
02 | local hum = script.Parent.Parent.Parent:FindFirstChild( "Humanoid" ) |
03 | stunned = false |
04 |
05 | if buildup.Value < = 7 then |
06 | if stunned = = false then |
07 | stunned = true |
08 | print ( "epic" ) |
09 | local StunAnim = hum:LoadAnimation(script.Parent.StunAnim) |
10 | StunAnim:Play() |
11 | hum.WalkSpeed = 0 |
12 | if stunned = = true then |
13 | wait( 5 ) |
14 | stunned = false |
15 | StunAnim:Stop() |
16 | hum.WalkSpeed = 16 |
17 | buildup.Value = 21 |
18 | end |
19 | end |
20 | end |
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:
01 | local buildup = script.Parent |
02 | local hum = script.Parent.Parent.Parent:WaitForChild( "Humanoid" ) --:FindFirstChild("Humanoid") |
03 | stunned = false |
04 |
05 | buildup.Changed:Connect( function (newValue) -- Whenever buildup's value is changed, this will get called. |
06 | if newValue < = 7 then -- 'newValue' is the value buildup was previously changed to |
07 | if stunned = = false then |
08 | stunned = true |
09 | print ( "epic" ) |
10 | local StunAnim = hum:LoadAnimation(script.Parent.StunAnim) |
11 | StunAnim:Play() |
12 | hum.WalkSpeed = 0 |
13 | if stunned = = true then |
14 | wait( 5 ) |
15 | stunned = false |
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.
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