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

My attack script not work and not have error in output, Why?

Hello i'm trying to make a attack script. The script works in studio but in the game not work :/ here it's the script:

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character
local debounceQ = true
local debounceTime = 0.5
local debounceE = true

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if debounceQ == true then
			if Player.CanAttack.Value == true then
				if Character ~= nil then
					if Character:FindFirstChild("Humanoid") then
						debounceQ = false
						local Humanoid = Character.Humanoid
						local Anim1 = Humanoid:LoadAnimation(script.AttackQ)
						local Anim2 = Humanoid:LoadAnimation(script.AttackE)
						Anim1:Play()
						Player.CanDamageQ.Value = true
						wait(debounceTime)
						Player.CanDamageQ.Value = false
						wait(0.4)
						debounceQ = true
					end
				end
			end
		end
	end
end)

Character:WaitForChild("Left Arm").Touched:connect(function(hit)
	if Player.CanDamageQ.Value == true then
		if hit.Parent:FindFirstChild("Humanoid") then
			local hum = hit.Parent.Humanoid
			hum:TakeDamage(5)
		end
	end
end)

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if debounceE == true then
			if Player.CanAttack.Value == true then
				if Character ~= nil then
					if Character:FindFirstChild("Humanoid") then
						debounceE = false
						local Humanoid = Character.Humanoid
						local Anim1 = Humanoid:LoadAnimation(script.AttackQ)
						local Anim2 = Humanoid:LoadAnimation(script.AttackE)
						Anim2:Play()
						Player.CanDamageE.Value = true
						wait(debounceTime)
						Player.CanDamageE.Value = false
						wait(0.4)
						debounceE = true
					end
				end
			end
		end
	end
end)

Character:WaitForChild("Right Arm").Touched:connect(function(hit)
	if Player.CanDamageE.Value == true then
		if hit.Parent:FindFirstChild("Humanoid") then
			local hum = hit.Parent.Humanoid
			hum:TakeDamage(5)
		end
	end
end)

pls help me!

2 comments

0

USE WAITFORCHILD SO IT CAN LOAD

0

"The script works in studio but in the game not work" is very broad.

1 answer

I guess it's a server-side script... Well, I think the error is that you got the player with

game.Players.LocalPlayer

which is wrong.

LocalPlayer can only be used in LocalScripts, you have to use a way to get the player. Here's how I would do it:

game.Players.PlayerAdded:Connect(function(Player)
	local UIS = game:GetService("UserInputService")
	local Mouse = Player:GetMouse()
	local Character = Player.Character
	local debounceQ = true
	local debounceTime = 0.5
	local debounceE = true
	
	UIS.InputBegan:connect(function(input)
	    if input.KeyCode == Enum.KeyCode.Q then
	        if debounceQ == true then
	            if Player.CanAttack.Value == true then
	                if Character ~= nil then
	                    if Character:FindFirstChild("Humanoid") then
	                        debounceQ = false
	                        local Humanoid = Character.Humanoid
	                        local Anim1 = Humanoid:LoadAnimation(script.AttackQ)
	                        local Anim2 = Humanoid:LoadAnimation(script.AttackE)
	                        Anim1:Play()
	                        Player.CanDamageQ.Value = true
	                        wait(debounceTime)
	                        Player.CanDamageQ.Value = false
	                        wait(0.4)
	                        debounceQ = true
	                    end
	                end
	            end
	        end
	    end
	end)
	
	Character:WaitForChild("Left Arm").Touched:connect(function(hit)
	    if Player.CanDamageQ.Value == true then
	        if hit.Parent:FindFirstChild("Humanoid") then
	            local hum = hit.Parent.Humanoid
	            hum:TakeDamage(5)
	        end
	    end
	end)
	
	UIS.InputBegan:connect(function(input)
	    if input.KeyCode == Enum.KeyCode.E then
	        if debounceE == true then
	            if Player.CanAttack.Value == true then
	                if Character ~= nil then
	                    if Character:FindFirstChild("Humanoid") then
	                        debounceE = false
	                        local Humanoid = Character.Humanoid
	                        local Anim1 = Humanoid:LoadAnimation(script.AttackQ)
	                        local Anim2 = Humanoid:LoadAnimation(script.AttackE)
	                        Anim2:Play()
	                        Player.CanDamageE.Value = true
	                        wait(debounceTime)
	                        Player.CanDamageE.Value = false
	                        wait(0.4)
	                        debounceE = true
	                    end
	                end
	            end
	        end
	    end
	end)
	
	Character:WaitForChild("Right Arm").Touched:connect(function(hit)
	    if Player.CanDamageE.Value == true then
	        if hit.Parent:FindFirstChild("Humanoid") then
	            local hum = hit.Parent.Humanoid
	            hum:TakeDamage(5)
	        end
	    end
	end)
end)

2 comments

0

@XenofTheInmortal still not work in the game

0

Try to use WaitForChild maybe it fired before the game generated.

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