Hey, it seems like this problem is taking a while to solve.
My assumption after removing the CharacterData, Animations and Hitbox from the code so I can test it myself is that the suspected scope, UIS InputBegan event, is not the problem, as it worked normally.
Basically, this works:
local UIS = game:GetService("UserInputService")
local punchInput = Enum.UserInputType.MouseButton1
local players = game:GetService("Players")
local attacking = false
local count = 0
local oldCount
local alreadyHit = { }
local char = players.LocalPlayer.Character
local resetTime = 2
--local CharacterData = game.Players.LocalPlayer.CharacterData
local animLength = 0.5
local animLength2 = 1
local animator = char.Humanoid.Animator
--local Animations = game.ReplicatedStorage.Animations
--local MOVESET_ANIMATIONS: { [string]: { Animation } } = table.freeze{
-- Mokou = { Animations.Mokou.Punch1, Animations.Mokou.Punch2, Animations.Mokou.Punch3, Animations.Mokou.Punch4 },
-- Sen = { },
-- Sakuya = { }
--}
--local moveset: { Animation }? = MOVESET_ANIMATIONS[CharacterData.Moveset]
--if not moveset then
-- error("Moveset not found!")
--end
--local punchAnimations: { AnimationTrack } = {}
--for index: number, animation: Animation in moveset do
-- punchAnimations[index] = animator:LoadAnimation(animation)
--end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == punchInput then
if attacking == true then
print("Already punching!")
elseif attacking == false then
count = count + 1
currentTime = tick()
print("M1 count is now "..count)
--punchAnimations[count].Priority = Enum.AnimationPriority.Action
--punchAnimations[count]:Play()
--char.M1Box.CanTouch = true
attacking = true
if count <= 3 then
task.wait(animLength)
--char.M1Box.CanTouch = false
attacking = false
elseif count >= 4 then
task.wait(animLength2)
--char.M1Box.CanTouch = false
attacking = false
count = 0
end
end
end
end)
task.spawn(function()
while true do
task.wait(0.25)
if count > 0 then
if tick() - currentTime >= resetTime then
count = 0
end
end
end
end)
--char.M1Box.Touched:Connect(function(hit)
-- print("touch event fired")
-- if hit.Parent.Humanoid ~= nil then
-- print("humanoid exists!")
-- if not table.find(alreadyHit, hit.Parent.Humanoid) then
-- print("it's not in the table, inserting")
-- table.insert(alreadyHit, hit.Parent.Humanoid)
-- print("Event passing...")
-- punchEvent:FireServer(hit.Parent.Humanoid, count)
-- print("Event sent!")
-- task.wait(animLength)
-- table.remove(alreadyHit, table.find(alreadyHit, hit.Parent.Humanoid)) -- OH MY GOD WHY ISNT THIS BUILT INTO THE FUNCTION
-- print("Removed")
-- end
-- end
--end)
I also took my time implementing the hitbox again, so the only things that aren’t included in my modified version of your script are: Animations and CharacterData. This is the version with hitbox:
local UIS = game:GetService("UserInputService")
local punchInput = Enum.UserInputType.MouseButton1
local players = game:GetService("Players")
local attacking = false
local count = 0
local oldCount
local alreadyHit = { }
local char = players.LocalPlayer.Character
local resetTime = 2
--local CharacterData = game.Players.LocalPlayer.CharacterData
local animLength = 0.5
local animLength2 = 1
local animator = char.Humanoid.Animator
--local Animations = game.ReplicatedStorage.Animations
--local MOVESET_ANIMATIONS: { [string]: { Animation } } = table.freeze{
-- Mokou = { Animations.Mokou.Punch1, Animations.Mokou.Punch2, Animations.Mokou.Punch3, Animations.Mokou.Punch4 },
-- Sen = { },
-- Sakuya = { }
--}
--local moveset: { Animation }? = MOVESET_ANIMATIONS[CharacterData.Moveset]
--if not moveset then
-- error("Moveset not found!")
--end
--local punchAnimations: { AnimationTrack } = {}
--for index: number, animation: Animation in moveset do
-- punchAnimations[index] = animator:LoadAnimation(animation)
--end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == punchInput then
if attacking == true then
print("Already punching!")
elseif attacking == false then
count = count + 1
currentTime = tick()
print("M1 count is now "..count)
--punchAnimations[count].Priority = Enum.AnimationPriority.Action
--punchAnimations[count]:Play()
char.M1Box.CanTouch = true
attacking = true
if count <= 3 then
task.wait(animLength)
char.M1Box.CanTouch = false
attacking = false
elseif count >= 4 then
task.wait(animLength2)
char.M1Box.CanTouch = false
attacking = false
count = 0
end
end
end
end)
task.spawn(function()
while true do
task.wait(0.25)
if count > 0 then
if tick() - currentTime >= resetTime then
count = 0
end
end
end
end)
char.M1Box.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not table.find(alreadyHit, hit.Parent.Humanoid) then
table.insert(alreadyHit, hit.Parent.Humanoid)
punchEvent:FireServer(hit.Parent.Humanoid, count, LAST_PUNCH_KNOCKBACK, LAST_PUNCH_UPWARDS_KNOCKBACK)
task.wait(animLength)
table.remove(alreadyHit, table.find(alreadyHit, hit.Parent.Humanoid)) -- OH MY GOD WHY ISNT THIS BUILT INTO THE FUNCTION
end
end
end)
That version considers that the hitbox is initially setup for every player in a ServerScript with the following code:
-- This code is purely debug-only and is not optimized for game development
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(character)
local hitbox = Instance.new("Part")
hitbox.Parent = character
hitbox.Name = "M1Box"
hitbox.Position = character:FindFirstChild("HumanoidRootPart").CFrame.Position + Vector3.new(0, 0, -3)
hitbox.Anchored = false
hitbox.CanCollide = false
hitbox.CanTouch = false
local weld = Instance.new("WeldConstraint")
weld.Part0 = character:FindFirstChild("HumanoidRootPart")
weld.Part1 = hitbox
weld.Parent = hitbox
end)
plr.CharacterRemoving:Connect(function(character)
if character:FindFirstChild("M1Box") then
character:FindFirstChild("M1Box"):Destroy()
end
end)
end)
With that code, your hitbox and UIS system should both be working fine. A few addition, you’re using .Touched() which is not recommended for hitboxes, specially ones that only need a single check and done. You currently need to turn .CanTouch on and off multiple times, which is not efficient nor works better for readability. If I was to choose something for this, I’d use workspace:GetPartBoundsInBox(). In basical form, you give it a Position and a Size, and it returns an array of whatever instances are currently inside that box. There are some other optimizations that I won’t touch due to possible opinion on code design.
After the previous test, I tested animations:
local UIS = game:GetService("UserInputService")
local punchInput = Enum.UserInputType.MouseButton1
local players = game:GetService("Players")
local attacking = false
local count = 0
local oldCount
local alreadyHit = { }
local char = players.LocalPlayer.Character
local resetTime = 2
--local CharacterData = game.Players.LocalPlayer.CharacterData
local animLength = 0.5
local animLength2 = 1
local animator = char.Humanoid.Animator
local Animations = game.ReplicatedStorage
local MOVESET_ANIMATIONS: { [string]: { Animation } } = table.freeze{
Mokou = { Animations.Mokou.Punch1, Animations.Mokou.Punch2, Animations.Mokou.Punch3, Animations.Mokou.Punch4 },
Sen = { },
Sakuya = { }
}
local moveset: { Animation }? = MOVESET_ANIMATIONS["Mokou"]
if not moveset then
error("Moveset not found!")
end
local punchAnimations: { AnimationTrack } = {}
for index: number, animation: Animation in moveset do
punchAnimations[index] = animator:LoadAnimation(animation)
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == punchInput then
if attacking == true then
print("Already punching!")
elseif attacking == false then
count = count + 1
currentTime = tick()
print("M1 count is now "..count)
punchAnimations[count].Priority = Enum.AnimationPriority.Action
punchAnimations[count]:Play()
char.M1Box.CanTouch = true
attacking = true
if count <= 3 then
task.wait(animLength)
char.M1Box.CanTouch = false
attacking = false
elseif count >= 4 then
task.wait(animLength2)
char.M1Box.CanTouch = false
attacking = false
count = 0
end
end
end
end)
task.spawn(function()
while true do
task.wait(0.25)
if count > 0 then
if tick() - currentTime >= resetTime then
count = 0
end
end
end
end)
char.M1Box.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not table.find(alreadyHit, hit.Parent.Humanoid) then
table.insert(alreadyHit, hit.Parent.Humanoid)
punchEvent:FireServer(hit.Parent.Humanoid, count, LAST_PUNCH_KNOCKBACK, LAST_PUNCH_UPWARDS_KNOCKBACK)
task.wait(animLength)
table.remove(alreadyHit, table.find(alreadyHit, hit.Parent.Humanoid)) -- OH MY GOD WHY ISNT THIS BUILT INTO THE FUNCTION
end
end
end)
What did I do? Check where I index the moveset from MOVESET_ANIMATIONS. All I did was change “CharacterData.Moveset” to specifically “Mokou”. This means that the CharacterData or CharacterData.Moveset is invalid. Like people said above, you likely tried to use StringValue as an index instead of StringValue.Value. This is it, your whole script, almost unchanged. The animations work, (I got an animation from the toolbox), everything. The only thing that isn’t here is the Event firing since I wasn’t feeling like replicating the events hierarchy in your game, which is unnecessary, since the server part seems to be working fine.
The algorithm is correct:
Create a read-only dictionary that consists of MovesetName = Animation
Gets the moveset of the Player’s CharacterData
If that moveset isn’t in MOVESET_ANIMATIONS (so if it is nil) then error out
Initialize an array called punchAnimations to contain loaded AnimationTracks
Finally, iterate thru every animation that the moveset has and add it to the punchAnimations array.
I also don’t like to leave my jobs half done, so I made the knockback system work for you. Here is the fixed ServerScript and LocalScript:
LocalScript
local UIS = game:GetService("UserInputService")
local punchEvent = game:GetService("ReplicatedStorage").Events:WaitForChild("PunchEvent")
local walkspeedChanger = game:GetService("ReplicatedStorage").Events:WaitForChild("WalkspeedChanger")
local punchInput = Enum.UserInputType.MouseButton1
local players = game:GetService("Players")
local attacking = false
local count = 0
local oldCount
local alreadyHit = { }
local char = players.LocalPlayer.Character
local resetTime = 2
local LAST_PUNCH_KNOCKBACK = 1700
local LAST_PUNCH_UPWARDS_KNOCKBACK = 1000
--local CharacterData = game.Players.LocalPlayer.CharacterData
local animLength = 0.5
local animLength2 = 1
local animator = char.Humanoid.Animator
local Animations = game.ReplicatedStorage.Animations
local MOVESET_ANIMATIONS: { [string]: { Animation } } = table.freeze{
Mokou = { Animations.Mokou.Punch1, Animations.Mokou.Punch2, Animations.Mokou.Punch3, Animations.Mokou.Punch4 },
Sen = { },
Sakuya = { }
}
local moveset: { Animation }? = MOVESET_ANIMATIONS["Mokou"]
if not moveset then
error("Moveset not found!")
end
local punchAnimations: { AnimationTrack } = {}
for index: number, animation: Animation in moveset do
punchAnimations[index] = animator:LoadAnimation(animation)
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == punchInput then
if attacking == true then
elseif attacking == false then
count = count + 1
currentTime = tick()
punchAnimations[count].Priority = Enum.AnimationPriority.Action
punchAnimations[count]:Play()
char.M1Box.CanTouch = true
attacking = true
if count <= 3 then
task.wait(animLength)
char.M1Box.CanTouch = false
attacking = false
elseif count >= 4 then
walkspeedChanger:FireServer(count, animLength2)
task.wait(animLength2)
char.M1Box.CanTouch = false
attacking = false
count = 0
end
end
end
end)
task.spawn(function()
while true do
task.wait(0.25)
if count > 0 then
if tick() - currentTime >= resetTime then
count = 0
end
end
end
end)
char.M1Box.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not table.find(alreadyHit, hit.Parent.Humanoid) then
table.insert(alreadyHit, hit.Parent.Humanoid)
punchEvent:FireServer(hit.Parent.Humanoid, count, LAST_PUNCH_KNOCKBACK, LAST_PUNCH_UPWARDS_KNOCKBACK)
task.wait(animLength)
table.remove(alreadyHit, table.find(alreadyHit, hit.Parent.Humanoid)) -- OH MY GOD WHY ISNT THIS BUILT INTO THE FUNCTION
end
end
end)
Server Script
local punchEvent = game:GetService("ReplicatedStorage").Events:WaitForChild("PunchEvent")
local WalkspeedChanger = game:GetService("ReplicatedStorage").Events:WaitForChild("WalkspeedChanger") --might be unneeded but i can't test that so here it is... sigh
punchEvent.OnServerEvent:Connect(function(plr: Player, targetHum: Humanoid, count: number, FORCE_MULTIPLIER: number, UP_FORCE: number)
local plrChar = plr.Character
local plrHrp: BasePart = plrChar:FindFirstChild("HumanoidRootPart")
local targetChar = targetHum.Parent
local targetHrp: BasePart = targetChar:FindFirstChild("HumanoidRootPart")
if count == 1 or count == 2 then
targetHum.Health -= 3
elseif count == 3 then
targetHum.Health -= 4
elseif count == 4 then
targetHum.Health -= 4
local direction = (targetHrp.Position-plrHrp.Position).Unit
local initForce = (direction * FORCE_MULTIPLIER)
local finalForce = Vector3.new(initForce.X, 1, initForce.Z) * Vector3.new(1, UP_FORCE, 1)
targetHrp:ApplyImpulse(finalForce)
print(finalForce)
elseif count >= 5 then
print("how the hell")
end
end)
WalkspeedChanger.OnServerEvent:Connect(function(plr, count, waitTime)
if count == 4 then --double check (wont prevent exploiting anyways but whatever...)
plr.Character.Humanoid.WalkSpeed = 0
task.wait(waitTime + 0.3)
plr.Character.Humanoid.WalkSpeed = 32
end
end)
To get the direction of the knockback, you don’t need the lookVector: I simply just did
local initForce = (targetHrp.Position - plrHrp.Position).Unit
Vector3.Unit is simply a normalized copy of the Vector with a magnitude (that is, a distance between these 2 values), of 1, so it simply returns that same Vector3, but withouth a length, only a direction. It’s like saying: (X = 0.5, Y = 0, Z = 0.5) This is aiming half at the X value, half at the Z value and is not aiming upwards at all. That, when multiplied by a multiplier, gives the Vector3 positioned in the world in studs, which is going to be that multiplier amount, in that direction.
After that, I create a new Vector3 value called “finalForce”, which only exists to apply the upwards force.
local finalForce = Vector3.new(initForce.X, 1, initForce.Z) * Vector3.new(1, UP_FORCE, 1)
It creates a new Vector3 that has the X value of the initForce, same for Z value, but a Y value of 1: which means that it is pointing towards the Y value. I then multiply it by a Vector3 of X value 1, Z value 1, and Y value the UP_FORCE. In that equation, what I’m saying is: “Make it so the force now also, not only having the X and Z values, but also having the value of UP_FORCE in the Y axis, while maintaining the X and Z values of initForce.”
OBS: You have to include the FireHandler and RagdollSystem since I do not have access to these. You also need to fix the CharacterData.Moveset problem mentioned earlier. Also, in the .Touched event for the hitbox in the local script, if hit.Parent.Humanoid then
does not work, as you’re trying to get that Humanoid directly, and that gives an error. I changed it to if hit.Parent:FindFirstChild("Humanoid") then, which can then be evaluated if found or not found, since if not found, it returns nil instead of erroring.
I have included the forces in the LocalScript: You can tweak to your likings.
NOTE:
This code review and debugging was done at 2 AM. Please consider reading the full post as I worked hard for it
. I like to help other programmers simply because when I needed help, I didn’t have access to the forum due to not have read enough. Also, I always try to explain my rewritten code as best as I can, including explaining what was wrong and what wasn’t, and I’m open for questions, even if the script works. My goal here is to teach, not to distribute code.