Hello, I’m trying to make a script that allows player to enable/disable low gravity in a gui, but I’m currently having problems with it because once the player died their JumpPower goes back to normal.
This is a local script. Here’s what is inside:
local LocalPlayer = game:GetService("Players").LocalPlayer
local char = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Text == "Low Gravity: Off" then
script.Parent.Text = "Low Gravity: On"
char.Humanoid.JumpPower = 75
elseif script.Parent.Text == "Low Gravity: On" then
script.Parent.Text = "Low Gravity: Off"
char.Humanoid.JumpPower = 50
end
end)
My question is, how would I make it so their jumppower stays the same after they die? Any responses are helpful, thanks.
You can use Player.CharacterAdded. This function will run whenever a new character is added into the game. When a player dies, their old character gets destroyed and a new one spawns. Once you have fired the character added event, change the jumppower there. This will update the jumppower when the character spawns, as all the values reset when a character dies.
Try:
local LocalPlayer = game:GetService("Players").LocalPlayer
LocalPlayer.CharacterAdded:Connect(function(Character)
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Text == "Low Gravity: Off" then
script.Parent.Text = "Low Gravity: On"
char.Humanoid.JumpPower = 75
elseif script.Parent.Text == "Low Gravity: On" then
script.Parent.Text = "Low Gravity: Off"
char.Humanoid.JumpPower = 50
end
end)
This should run the code and set the jumppower every time a character respawns.
You can save the player’s gravity with an IntValue inserted under the ReplicatedStorage and when they die you can replace their gravity with the IntValue.
This changes the jump power locally so it is not registered by the server.
You can change it to when the player clicks the button, it will fire a remote event which you can then use to either change a variable created within the server script on an IntValue to change the JumpPower whenever the Character is added.