Why won’t my code work? (ignore the numbers)

--// Services
local UserInputService = game:GetService('UserInputService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Players = game:GetService('Players')

--// Debounces
local Punching = false
local Damaging = true

--// Customisation
local Damage = 20
local Cooldown = 0.5

--// Events
local PunchEvent = ReplicatedStorage:WaitForChild('PunchEvent')

--// Player
local Player = game.Players.LocalPlayer
local Character = game.Workspace:WaitForChild(Player.Name)
local Humanoid = Character:FindFirstChildOfClass('Humanoid')
local RightArm = Character:WaitForChild('Right Arm')

--//Animations
Punch = Instance.new('Animation')
Punch.AnimationId = 'rbxassetid://953446258'
PunchTrack = Humanoid:LoadAnimation(Punch)

--// Coding
UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E and not Punching then
		Punching = true
		PunchTrack:Play()
		PunchTrack.KeyframeReached:Connect(function(Keyframe)
			if Keyframe == 'End' then
				wait(Cooldown)
				Punching = false
				Damaging = true
			end
		end)
	end
end)

RightArm.Touched:Connect(function(hitPart)
	if Punching and hitPart.Parent:FindFirstChild('Humanoid') and Damaging then
		Damaging = false
		local HumanoidToDamage = hitPart.Parent:FindFirstChild('Humanoid')
		PunchEvent:FireServer(HumanoidToDamage, Damage)
	end
end)

Can you give more context? What do you mean when you say your code doesn’t work?
What are you expecting to happen, what’s happening instead, what have you tried?
What’s the code supposed to do? Are there any errors?

2 Likes

When I go it’s and test it doesn’t work. Can you guys try the code? And show me a screenshot?

To test the code we need the elements to which the script refers. Make a screenshot of Output window or send a place file.

I managed to get the animation part of the script working. I had to change the

local RightArm = Character:WaitForChild('Right Arm')

to this

local RightArm = Character:WaitForChild('RightHand')

So perhaps your using R15 in your place as well? After I made that fix it seems to work fine.

Wait, I put this
PunchTrack = Humanoid:LoadAnimation(Punch)
Now it’s fixed, sorry about the trouble.

Wait, that is it, it was so obviously. That’s why the Humanoid kept saying error when I thought I had it right.

[quote="amifat2, post:1, topic:594351, full:true"]
--// Services
    local UserInputService = game:GetService('UserInputService')
    local ReplicatedStorage = game:GetService('ReplicatedStorage')
    local Players = game:GetService('Players')

--// Debounces
local Punching = false
local Damaging = true

--// Customisation
local Damage = 20
local Cooldown = 0.5

--// Events
local PunchEvent = ReplicatedStorage:WaitForChild('PunchEvent')

--// Player
local Player = game.Players.LocalPlayer
local Character = game.Workspace:WaitForChild(Player.Name)
local Humanoid = Character:FindFirstChildOfClass('Humanoid')
local RightArm = Character:WaitForChild('Right Arm')

--//Animations
Punch = Instance.new('Animation')
Punch.AnimationId = 'rbxassetid://953446258'
PunchTrack = Humanoid:LoadAnimation(Punch)

--// Coding
UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E and not Punching then
		Punching = true
		PunchTrack:Play()
		PunchTrack.KeyframeReached:Connect(function(Keyframe)
			if Keyframe == 'End' then
				wait(Cooldown)
				Punching = false
				Damaging = true
			end
		end)
	end
end)

RightArm.Touched:Connect(function(hitPart)
	if Punching and hitPart.Parent:FindFirstChild('Humanoid') and Damaging then
		Damaging = false
		local HumanoidToDamage = hitPart.Parent:FindFirstChild('Humanoid')
		PunchEvent:FireServer(HumanoidToDamage, Damage)
	end
end)

[/quote] If it is in the same post can we have a anti cheat code for a daily log in bonus?