Hi! I’m new at scripting so I really don’t want to start a post without checking and searching for answers that have the same problem as me, but so far I haven’t found anything that works. So here I am:
-
What do I want to achieve?
– I just want to code to work after a player dies/reset -
What is the issue?
– The code works perfectly when I run it the first time, but whenever the player dies the code just stops at the Touched event. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
– I was thinking about disconnecting the Touch Event when a player dies but I’m not sure how to go about it in my code.
robloxapp-20200607-1014222.wmv (3.6 MB)
–I was clicking and pressing keys after I reset but nothing is happening
–I also tried putting it in StarterPLayerScripts ->the script didn’t work entirely
HERE’S MY CODE:
FIRST TRY:
Summary
----------//Service\\-----------------
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local fightModule = require(script.Parent:WaitForChild("FightScript"))
local UserInputService = game:GetService("UserInputService")
----------//Variables\\-----------------
local jab = script:WaitForChild("Jab")
local rightHook = script:WaitForChild("RightHook")
local upperCut = script:WaitForChild("UpperCut")
local block = script:WaitForChild("Block")
local enabled = false
local cooldown = 0.5
local damage
local FIGHT_ANIM = "Fighting Animation"
----------//Bind Function\\-----------------
local function fightAnim(actionName, inputState, inputObj)
local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
if inputState == Enum.UserInputState.Begin and enabled == false and inputObj.UserInputType == Enum.UserInputType.MouseButton1 then
enabled = true
local anim = humanoid:LoadAnimation(jab)
anim:Play()
fightModule.Fight(anim.Name)
wait(cooldown)
enabled = false
end
if inputObj.KeyCode == Enum.KeyCode.F and enabled == false then
enabled = true
local anim = humanoid:LoadAnimation(rightHook)
anim:Play()
fightModule.Fight(anim.Name)
wait(cooldown)
enabled = false
end
if inputObj.KeyCode == Enum.KeyCode.E and enabled == false then
enabled = true
local anim = humanoid:LoadAnimation(upperCut)
anim:Play()
fightModule.Fight(anim.Name)
wait(cooldown)
enabled = false
end
if inputObj.KeyCode == Enum.KeyCode.Space then
local anim = humanoid:LoadAnimation(block)
UserInputService.InputBegan:Connect(function()
if not UserInputService:IsKeyDown(Enum.KeyCode.Space) then
if anim.IsPlaying then
anim:Stop()
fightModule.Fight(anim.Name)
end
else
anim:Play()
fightModule.Fight(anim.Name)
end
end)
end
end
----------//Touched Functions\\-----------------
local function onTouched( limb )
return function ( hit )
if hit.Name == "sparStage" then
print(hit.Name) -----Does not work up to here when player dies/resets
ContextActionService:BindAction(FIGHT_ANIM, fightAnim, false, Enum.UserInputType.MouseButton1, Enum.KeyCode.F, Enum.KeyCode.E, Enum.KeyCode.Space)
end
end
end
local function onTouchEnded( limb )
return function ( hit )
if hit.Name == "sparStage" then
ContextActionService:UnbindAction(FIGHT_ANIM)
end
end
end
----------//Checking Players\\-----------------
local function onCharacterAdded( chr )
local hum = chr:FindFirstChild( "Humanoid" )
if hum then
local leftFoot = chr:FindFirstChild( "LeftFoot" )
local rightFoot = chr:FindFirstChild( "RightFoot" )
local upperTorso = chr:FindFirstChild( "UpperTorso" )
leftFoot.Touched:Connect( onTouched( leftFoot ) )
leftFoot.TouchEnded:Connect( onTouchEnded( leftFoot ) )
rightFoot.Touched:Connect( onTouched( rightFoot ) )
rightFoot.TouchEnded:Connect( onTouchEnded( rightFoot ) )
upperTorso.Touched:Connect( onTouched( upperTorso ) )
upperTorso.TouchEnded:Connect( onTouchEnded( upperTorso ) )
print(chr.Name) ---Just used this to see if it works up to here
end
end
local function onPlayerAdded(player)
-- Check if they already spawned in
if player.Character then
onCharacterAdded(player.Character)
end
-- Listen for the player (re)spawning
player.CharacterAdded:Connect(onCharacterAdded)
end
-- Iterate over each player already connected
-- to the game using a generic for-loop
for i, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
-- Listen for newly connected players
Players.PlayerAdded:Connect(onPlayerAdded)
**UPDATED: I remove the player added codes but still got the problem
UPDATED CODE:
Summary
----------//Service\\-----------------
local ContextActionService = game:GetService("ContextActionService")
local fightModule = require(script.Parent:WaitForChild("FightScript"))
local UserInputService = game:GetService("UserInputService")
----------//Variables\\-----------------
local Player = game.Players.LocalPlayer
local Character = Player.Character
local jab = script:WaitForChild("Jab")
local rightHook = script:WaitForChild("RightHook")
local upperCut = script:WaitForChild("UpperCut")
local block = script:WaitForChild("Block")
local enabled = false
local cooldown = 0.5
local damage
local FIGHT_ANIM = "Fighting Animation"
----------//Bind Function\\-----------------
local function fightAnim(actionName, inputState, inputObj)
local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
if inputState == Enum.UserInputState.Begin and enabled == false and inputObj.UserInputType == Enum.UserInputType.MouseButton1 then
enabled = true
local anim = humanoid:LoadAnimation(jab)
anim:Play()
fightModule.Fight(anim.Name)
wait(cooldown)
enabled = false
end
if inputObj.KeyCode == Enum.KeyCode.F and enabled == false then
enabled = true
local anim = humanoid:LoadAnimation(rightHook)
anim:Play()
fightModule.Fight(anim.Name)
wait(cooldown)
enabled = false
end
if inputObj.KeyCode == Enum.KeyCode.E and enabled == false then
enabled = true
local anim = humanoid:LoadAnimation(upperCut)
anim:Play()
fightModule.Fight(anim.Name)
wait(cooldown)
enabled = false
end
if inputObj.KeyCode == Enum.KeyCode.Space then
local anim = humanoid:LoadAnimation(block)
UserInputService.InputBegan:Connect(function()
if not UserInputService:IsKeyDown(Enum.KeyCode.Space) then
if anim.IsPlaying then
anim:Stop()
fightModule.Fight(anim.Name)
end
else
anim:Play()
fightModule.Fight(anim.Name)
end
end)
end
end
----------//Touched Functions\\-----------------
local function onTouched( limb )
print(limb.Name)
return function ( hit )
if hit.Name == "sparStage" then
print(hit.Name) -----Does not work up to here when player dies/resets
ContextActionService:BindAction(FIGHT_ANIM, fightAnim, false, Enum.UserInputType.MouseButton1, Enum.KeyCode.F, Enum.KeyCode.E, Enum.KeyCode.Space)
end
end
end
local function onTouchEnded( limb )
return function ( hit )
if hit.Name == "sparStage" then
ContextActionService:UnbindAction(FIGHT_ANIM)
end
end
end
----------//Checking Players\\-----------------
local function onCharacterAdded( chr )
local hum = chr:WaitForChild( "Humanoid" )
if hum then
local leftFoot = chr:FindFirstChild( "LeftFoot" )
local rightFoot = chr:FindFirstChild( "RightFoot" )
local upperTorso = chr:FindFirstChild( "UpperTorso" )
leftFoot.Touched:Connect( onTouched( leftFoot ) )
leftFoot.TouchEnded:Connect( onTouchEnded( leftFoot ) )
rightFoot.Touched:Connect( onTouched( rightFoot ) )
rightFoot.TouchEnded:Connect( onTouchEnded( rightFoot ) )
upperTorso.Touched:Connect( onTouched( upperTorso ) )
upperTorso.TouchEnded:Connect( onTouchEnded( upperTorso ) )
print(chr.Name)
end
end
-- Check if they already spawned in
if Character then
onCharacterAdded(Character)
end
-- Listen for the player (re)spawning
Player.CharacterAdded:Connect(onCharacterAdded)
Any help is appreciated! Thank you so much in advance!