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

How do I make Jump Power not reset on death?

leaderstats script

local prest = workspace:WaitForChild("Prestige")
local close = workspace:WaitForChild("Close")
game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new('Model')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player
	local reb = Instance.new("IntValue", leaderstats)
	reb.Name = "Rebirths"
	reb.Value = 0
	local costmult = Instance.new("IntValue", player)
	costmult.Value = 1
	costmult.Name = "costmult"
    local JumpPower = Instance.new('NumberValue')
    local char = player.Character or player.CharacterAdded:Wait()
    local hum = char.Humanoid
    JumpPower.Value= 0 
    JumpPower.Parent=leaderstats
    JumpPower.Name='Score'
    hum.Changed:Connect(function()
   		JumpPower.Value = 0 + math.floor((hum.JumpPower*20-1000)+ 0.5)	
	end)
	player.Character.Humanoid.Died:Connect(function()
		player.CharacterAdded:Connect(function()
			player.Character.Humanoid.JumpPower = JumpPower.Value/20+50+(0.05*((player.leaderstats.Rebirths.Value+1)))-0.05
		end)
		
	end)
	prest.onServerEvent:Connect(function(plr)
		print(plr.costmult.Value)
		if plr.leaderstats.Score.Value >= (1500*plr.costmult.Value) then
			close:FireClient(plr)
			plr.Character.Humanoid.JumpPower = 50
			plr.leaderstats.Score.Value = plr.leaderstats.Score.Value- (1500*costmult.Value)
			plr.leaderstats.Rebirths.Value = plr.leaderstats.Rebirths.Value +1
			costmult.Value = costmult.Value*2
			JumpPower.Value = 0 + math.floor((hum.JumpPower*20-1000)+ 0.5)
		end
	end)
end)

Points change script


local ReplicatedStorage = game:GetService('ReplicatedStorage')


ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
    player.Character.Humanoid.JumpPower = (player.Character.Humanoid.JumpPower + (0.05*((player.leaderstats.Rebirths.Value+1))))
print(player.costmult.Value)
end)

This is the part of the leaderstats script I tried to preserve the Jump Power of the player

player.Character.Humanoid.Died:Connect(function()
		player.CharacterAdded:Connect(function()
			player.Character.Humanoid.JumpPower = JumpPower.Value/20+50+(0.05*((player.leaderstats.Rebirths.Value+1)))-0.05
		end)

This script only works once then the Points on the leaderboard don't update. Also if they die for a second time it will revert their jump power to the value it was when they died for the first time.

1 answer

✓ Accepted answer

Improvements

Use :WaitForChild() to make sure a part exists before referencing it in your code

Use :GetService() for the Players Service

It's better for your game's RAM to set the parent of a new instance after its properties are set

Issues

For "prest" the "onServerEvent" should be capitalized to OnServerEvent (Lua is case sensitive)

OnServerEvent should occur outside of the Players' PlayerAdded function

CharacterAdded should not be called by the Died event, if anything, the Died event would be called from the CharacterAdded event as the current character would be referenced. This is because the Died event fires in relation with the removal of a character, and would be unable to reference the new character.

In the "prest" server event, you have the jumppower value set twice, this may be intentional, but the final output will be the last one (I've dashed out the first setting of the value as the rest of the code doesn't seem to need to reference it.

If the Humanoid's JumpPower is supposed to be based on the JumpPower number value, you may want to add the following:

JumpPower:GetPropertyChangedSignal("Value"):Connect(function()
	player.Character:WaitForChild("Humanoid").JumpPower = JumpPower.Value
end)

inside the PlayerAdded function after the creation of the "JumpPower" variable

Revised Server Script #1

local prest = workspace:WaitForChild("Prestige")
local close = workspace:WaitForChild("Close")

game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Model")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local reb = Instance.new("IntValue")
	reb.Name = "Rebirths"
	reb.Value = 0
	reb.Parent = leaderstats
	
	local costmult = Instance.new("IntValue")
	costmult.Name = "costmult"
	costmult.Value = 1
	costmult.Parent = player
    
	local JumpPower = Instance.new("NumberValue")
	JumpPower.Name = "Score"
	JumpPower.Value = 0 
	JumpPower.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		hum:GetPropertyChangedSignal("JumpPower"):Connect(function()
			JumpPower.Value = math.floor(((hum.JumpPower * 20) - 1000) + 0.5)  
		end)
		
		hum.JumpPower = (JumpPower.Value / 20) + (0.05 * (reb.Value + 1)) + 49.95
	end)
end)

prest.OnServerEvent:Connect(function(plr)
	local costmult = plr:WaitForChild("costmult")
	local reb = plr:WaitForChild("leaderstats"):WaitForChild("Rebirths")
	local JumpPower = plr:WaitForChild("leaderstats"):WaitForChild("Score")
	local hum = plr.Character:WaitForChild("Humanoid")
	print(costmult)
	
	if JumpPower.Value >= (1500 * costmult.Value) then
		close:FireClient(plr)
		hum.JumpPower = 50
		-- JumpPower.Value = JumpPower.Value - (1500 * costmult.Value)
		reb.Value = reb.Value + 1
		costmult.Value = costmult.Value * 2
		JumpPower.Value = math.floor(((hum.JumpPower * 20) - 1000) + 0.5)
	end
end)

Revised Server Script #2

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage:WaitForChild("RemoteEvent").OnServerEvent:Connect(function(player)
	local hum = player.Character:WaitForChild("Humanoid")
	local reb = player:WaitForChild("leaderstats"):WaitForChild("Rebirths")
	local costmult = player:WaitForChild("costmult")
	
	hum.JumpPower = (hum.JumpPower + (0.05 * (reb.Value + 1)))
	print(costmult.Value)
end)

1 comment

0

Also I suggest if the points are updating, to make sure the "RemoteEvent" in the second Server Script is actually being fired

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