It would be better to use an event because an event runs when it is called in this case when the character loads in. By doing an if player.CharacterAdded
would only run once during a specific time or it wouldn’t run at all.
what if you made a function that ran every time CharacterAdded
happens?
I did up here
It fires when the player respawns but not when they load in because then there would be duplicate scripts.
couldnt you just destroy the script when they die and clone it back when the character is added again?
Your script is a little broken there no?
If you mean there are no areas that were indented then yes it is broken because the devforum doesn’t really give you the option to do that.
Edit: Just realized what you meant. I fixed it just now.
the issue is that the scripts aren’t coming back. It would be useless to clone the scripts after destroying them.
Hello there !
I just saw your problem and i’m going to explain why it happen and how to fix it.
Player Character
I saw that your Local Script is in “StarterPlayerScript”, so each script inside this storage are starting to run only one time (when you join the game).
That mean, all the variables you get at the top of the script are running only one time.
So when you’re getting the Player Character like this (code bellow), you get it only one time, this is the first spawned character when you’re joinning the game.
local PlayerService = game:GetService("Players")
local Client = PlayerService.LocalPlayer
local Character = Client.Character or Client.CharacterAdded:Wait()
When your character is dying, the Character model got deleted and a new one is created, but the problem here is like i said the script only get the first character, not the next one.
Fixing the problem
So, all you have to do is to update all the character variables you created at top of the script when a new character has spawned.
local PlayerService = game:GetService("Players")
local Client = PlayerService.LocalPlayer
local Character = Client.Character or Client.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart", 30)
local Hum = Character:WaitForChild("Humanoid", 30)
print(Character.Name, Root.Name, Hum.Name)
Client.CharacterAdded:Connect(function(NewCharacter)
local NewRoot = NewCharacter:WaitForChild("HumanoidRootPart", 30)
local NewHum = NewCharacter:WaitForChild("Humanoid", 30)
if NewRoot and NewHum then
Character = NewCharacter
Root = NewRoot
Hum = NewHum
end
print(Character.Name, Root.Name, Hum.Name)
end)
You also need to update all connections functions related to the old character, like humanoid.Running ect… here is your script:
local PlayerService = game:GetService("Players")
local Client = PlayerService.LocalPlayer
local Character = Client.Character or Client.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart", 30)
local Hum = Character:WaitForChild("Humanoid", 30)
local Footstep = Root and Root:WaitForChild("SteppingSound", 30)
local Connections = {}
local function onHumanoidRunning(Speed)
if Speed > 6 then
Footstep.Looped = true
Footstep:Play()
else
Footstep:Stop()
end
end
local function onHumanoidNotRunning()
Footstep.Looped = false
Footstep:Stop()
end
local function UpdateConnections()
Connections.Running = Hum.Running:Connect(onHumanoidRunning)
Connections.Jumping = Hum.Jumping:Connect(onHumanoidNotRunning)
Connections.FreeFalling = Hum.FreeFalling:Connect(onHumanoidNotRunning)
Connections.Swimming = Hum.Swimming:Connect(onHumanoidNotRunning)
end
Client.CharacterAdded:Connect(function(NewCharacter)
local NewRoot = NewCharacter:WaitForChild("HumanoidRootPart", 30)
local NewHum = NewCharacter:WaitForChild("Humanoid", 30)
local NewFootstep = NewRoot and NewRoot:WaitForChild("SteppingSound", 30)
if NewRoot and NewHum and NewFootstep then
Character = NewCharacter
Root = NewRoot
Hum = NewHum
Footstep = NewFootstep
end
UpdateConnections()
end)
UpdateConnections()
Hey, so the first script that updates all the character variables i need to make it a new script or put it at the top of each local scripts?
To keep your game optimized and save up lot of time, it is better to combine your 3 script into only one, so you don’t have to do the same things multiple times.
When you use the “:WaitForChild” function
the script won’t run until it find the child.
Example:
after you die you probably don’t have the foorstep sound inside of your humanoidrootpart
Alright, i finally finished that. Now where do i put the local Connections = {}
line and the Update Character Variables script?
No, the scripts completely dissapear after the player dies.
put them in started character scripts
Did that multiple times and it didn’t change anythin’.
Can you add me to the studio by any chance?
You can do something like this, i let you add the triple jump code into it by yourself, you just have to follow the script sheme/organization, it should be easy and understandable.
--//Services//--
local PlayerService = game:GetService("Players")
local TweenService = game:GetService("TweenService")
--//Main Variables//--
local Client = PlayerService.LocalPlayer
local Character = Client.Character or Client.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart", 30)
local Hum = Character:WaitForChild("Humanoid", 30)
--//Other Variables//--
local FootstepSound = Root and Root:WaitForChild("SteppingSound", 30)
local LandingSound = Root and Root:WaitForChild("LandingSound", 30)
local LandingAnim = script:WaitForChild("Landing", 30)
--//Tables//--
local Connections = {}
--//Local Functions//--
local function Running(Speed)
if Speed > 6 then
FootstepSound.Looped = true
FootstepSound:Play()
else
FootstepSound:Stop()
end
end
local function NotRunning()
FootstepSound:Stop()
end
local function Landing()
local Track = Hum:LoadAnimation(LandingAnim)
local CloudPart = Instance.new("Part")
local CloudMesh = Instance.new("SpecialMesh")
local TweenInfo1 = TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local TweenSize = TweenService:Create(CloudMesh, TweenInfo1, {Scale = Vector3.new(0.15, 0.025, 0.15)})
local TweenTransparency = TweenService:Create(CloudPart, TweenInfo1, {Transparency = 1})
Track:AdjustSpeed(5)
Track:Play()
LandingSound:Play()
---------------------------------------------------
CloudPart.Parent = Character
CloudPart.Anchored = true
CloudPart.CanCollide = false
CloudPart.CanTouch = false
CloudPart.Position = Root.Position - Vector3.new(0, 2.5, 0)
CloudPart.Size = Vector3.new(2.5, 2.5, 2.5)
CloudPart.Material = Enum.Material.Neon
CloudPart.BrickColor = BrickColor.new(255, 255, 255)
--------------------------------------------------------
CloudMesh.Parent = CloudPart
CloudMesh.Scale = Vector3.new(0.05, 0.025, 0.05)
CloudMesh.MeshId = "rbxassetid://11978515144"
--------------------------------------------------------
task.spawn(function()
TweenSize:Play()
TweenTransparency:Play()
task.wait(0.3)
CloudPart:Destroy()
end)
end
local function CheckState(OldState, NewState)
if NewState ~= Enum.HumanoidStateType.Running then
NotRunning()
end
if NewState == Enum.HumanoidStateType.Landed then
Landing()
end
end
local function UpdateConnections()
Connections.StateChanged = Hum.StateChanged:Connect(CheckState)
Connections.Running = Hum.Running:Connect(Running)
end
--//Main Functions//--
Client.CharacterAdded:Connect(function(NewCharacter)
local NewRoot = NewCharacter:WaitForChild("HumanoidRootPart", 30)
local NewHum = NewCharacter:WaitForChild("Humanoid", 30)
local NewFootstepSound = NewRoot and NewRoot:WaitForChild("SteppingSound", 30)
local NewLandingSound = NewRoot and NewRoot:WaitForChild("LandingSound", 30)
if NewRoot and NewHum and NewFootstepSound and NewLandingSound then
Character = NewCharacter
Root = NewRoot
Hum = NewHum
FootstepSound = NewFootstepSound
LandingSound = NewLandingSound
end
UpdateConnections()
end)
UpdateConnections()
This is annoying, even after all this it still doesn’t work. Here’s my code! No errors in output.
--Variables & Services--
---------------------------------------------------
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid", 30)
local Root = Character:WaitForChild("HumanoidRootPart", 30)
local Footstep = Root:WaitForChild("SteppingSound", 30)
local Landing = script:WaitForChild("Landing")
local LandingAnim = Humanoid:LoadAnimation(Landing)
local LandingSound = Root and Root:WaitForChild("LandingSound", 30)
local Step = Root and Root:WaitForChild("StepTJ", 30)
local Whoosh = Root and Root:WaitForChild("WhooshTJ", 30)
local Jump1 = script:WaitForChild("Jump 1")
local Jump2 = script:WaitForChild("Jump 2")
local Jump3 = script:WaitForChild("Jump 3")
local JumpAnim1 = Humanoid:LoadAnimation(Jump1)
local JumpAnim2 = Humanoid:LoadAnimation(Jump2)
local JumpAnim3 = Humanoid:LoadAnimation(Jump3)
---------------------------------------------------
local Connections = {}
---------------------------------------------------
--//Walking Sound//--
---------------------------------------------------
--Function--
function Running(Speed)
if Speed > 6 then
Footstep.Playing = true
else
Footstep.Playing = false
end
end
----------------------------------------------------
function NotRunning()
Footstep.Playing = false
end
---------------------------------------------------
--//Landing Effects//--
---------------------------------------------------
--Function--
function LandingFunction(_, State)
if State == Enum.HumanoidStateType.Landed then
LandingAnim:AdjustSpeed(5)
LandingAnim:Play()
LandingSound:Play()
---------------------------------------------------
local CloudPart = Instance.new("Part", Character)
CloudPart.Anchored = true
CloudPart.CanCollide = false
CloudPart.CanTouch = false
CloudPart.Position = Root.Position - Vector3.new(0, 2.5, 0)
CloudPart.Size = Vector3.new(2.5, 2.5, 2.5)
CloudPart.Material = Enum.Material.Neon
CloudPart.BrickColor = BrickColor.new(255, 255, 255)
--------------------------------------------------------
local CloudMesh = Instance.new("SpecialMesh", CloudPart)
CloudMesh.Scale = Vector3.new(0.05, 0.025, 0.05)
CloudMesh.MeshId = "rbxassetid://11978515144"
--------------------------------------------------------
local TweenService = game:GetService("TweenService")
local TweenInfo1 = TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local TweenSize = TweenService:Create(CloudMesh, TweenInfo1, {Scale = Vector3.new(0.15, 0.025, 0.15)})
local TweenTransparency = TweenService:Create(CloudPart, TweenInfo1, {Transparency = 1})
TweenSize:Play()
TweenTransparency:Play()
task.wait(0.3)
CloudPart:Destroy()
end
end
---------------------------------------------------
--Connection--
Humanoid.StateChanged:Connect(LandingFunction)
---------------------------------------------------
--//Triple Jump//--
JumpAnim1.Priority = Enum.AnimationPriority.Action3
JumpAnim2.Priority = Enum.AnimationPriority.Action3
JumpAnim3.Priority = Enum.AnimationPriority.Action3
----------------------------------------------------
Jump = 0
isJumping = false
isFalling = false
--Function--
function onJump(Val)
isJumping = Val
if isJumping and Jump > 1 and Jump < 3 then
Humanoid.JumpPower = 50
Jump = 3
JumpAnim3:Play()
JumpAnim3:AdjustSpeed(1.75)
Step:Play()
Whoosh:Play()
Root.Velocity = Root.Velocity + Vector3.new(0, 95, 0)
task.wait(0.2)
Whoosh:Play()
task.wait(0.35)
Whoosh:Play()
Jump = 0
elseif isJumping and Jump > 0 and Jump < 2 then
Humanoid.JumpPower = 50
Jump = 2
JumpAnim2:Play()
JumpAnim2:AdjustSpeed(0.5)
Step:Play()
Root.Velocity = Root.Velocity + Vector3.new(0, 75, 0)
elseif isJumping and Jump < 1 then
Humanoid.JumpPower = 50
Jump = 1
JumpAnim1:Play()
JumpAnim1:AdjustSpeed(0.75)
Step:Play()
end
end
---------------------------------------------
function onFall(Val)
isFalling = Val
if not Val then
task.wait(0.25)
if Jump > 0 and not isFalling then
Jump = 0
end
end
end
--Connection--
Humanoid.Jumping:Connect(onJump)
Humanoid.FreeFalling:Connect(onFall)
function CheckState(OldState, NewState)
if NewState ~= Enum.HumanoidStateType.Running then
NotRunning()
end
if NewState == Enum.HumanoidStateType.Landed then
LandingFunction()
end
end
---------------------------------------------------
function UpdateConnections()
Connections.StateChanged = Humanoid.StateChanged:Connect(CheckState)
Connections.Running = Humanoid.Running:Connect(Running)
end
---------------------------------------------------
Player.CharacterAdded:Connect(function(NewCharacter)
local NewRoot = NewCharacter:WaitForChild("HumanoidRootPart", 30)
local NewHum = NewCharacter:WaitForChild("Humanoid", 30)
local NewFootstepSound = NewRoot and NewRoot:WaitForChild("SteppingSound", 30)
local NewLandingSound = Root and Root:WaitForChild("LandingSound", 30)
if NewRoot and NewHum and NewFootstepSound and NewLandingSound then
Character = NewCharacter
Root = NewRoot
Humanoid = NewHum
Footstep = NewFootstepSound
LandingSound = NewLandingSound
end
UpdateConnections()
end)
--Connection--
UpdateConnections()
Did you tried the script i gived you in the previous message ? can you tell me if it work ?
Just with a copy/paste, without changing anything
Also there is your error:
Animations have to be loaded one by one, when you are about to play them, each time you load an animation it erase the previous one.
local JumpAnim1 = Humanoid:LoadAnimation(Jump1)
local JumpAnim2 = Humanoid:LoadAnimation(Jump2)
local JumpAnim3 = Humanoid:LoadAnimation(Jump3)
These event have to be connected to the connections table.
You can also delete the state changed as it already is called into the “UpdateConnections” function.
Humanoid.StateChanged:Connect(LandingFunction)
Humanoid.Jumping:Connect(onJump)
Humanoid.FreeFalling:Connect(onFall)
These variables have to be added into the main function at the bottom of the script.
local Step = Root and Root:WaitForChild("StepTJ", 30)
local Whoosh = Root and Root:WaitForChild("WhooshTJ", 30)