Gravity not changing after respawn

I have a zone in my game with gravity that will correspond to a gravity counter. For some reason, after i die in the game and come back to the zone, the gravity will not change to the counter’s number.

Gravity zone localscript (chatgpt cooked with this one):

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local regionPart = script.Parent:WaitForChild("RegionPart")
local defaultGravity = 196 -- Default Roblox gravity value

-- Function to check if the player is inside the region
local function isPlayerInRegion(character)
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if not humanoidRootPart then return false end

	local region = Region3.new(
		regionPart.Position - regionPart.Size / 2,
		regionPart.Position + regionPart.Size / 2
	)

	local partsInRegion = workspace:FindPartsInRegion3(region, nil, math.huge)
	for _, part in pairs(partsInRegion) do
		if part:IsDescendantOf(character) then
			return true
		end
	end
	return false
end

-- Function to update gravity
local function updateGravity(character)
	local gravityValue = regionPart:GetAttribute("CustomGravity") or defaultGravity
	if isPlayerInRegion(character) then
		workspace.Gravity = gravityValue
	else
		workspace.Gravity = defaultGravity
	end
end

-- Function to handle respawn and initial spawn
local function onCharacterAdded(character)
	-- Ensure the character exists and has the HumanoidRootPart
	character:WaitForChild("HumanoidRootPart")

	-- Monitor if the player enters or exits the region
	RunService.Heartbeat:Connect(function()
		updateGravity(character)
	end)
end

-- Connect to character respawn event
player.CharacterAdded:Connect(onCharacterAdded)

-- Handle the initial character if it exists
if player.Character then
	onCharacterAdded(player.Character)
end

-- Listen for changes to the CustomGravity attribute and update accordingly
regionPart:GetAttributeChangedSignal("CustomGravity"):Connect(function()
	if player.Character then
		updateGravity(player.Character)
	end
end)

Gravity setter localscript:

local intVal = script.Parent.Parent.GravityInt
local region = game.Workspace.Laboratory.GravityRoom.RegionPart

script.Parent.LowerButton.Activated:Connect(function()
	intVal.Value -= 10
	
	if intVal.Value < 16 then
		intVal.Value = 0
	elseif intVal.Value < 0 then
		intVal.Value = 0
	end
	
	script.Parent.ValueLabel.Text = tostring(intVal.Value)
	
	region:SetAttribute("CustomGravity", intVal.Value)
end)

script.Parent.HigherButton.Activated:Connect(function()
	intVal.Value += 10
	
	if intVal.Value > 306 then
		intVal.Value = 306
	end
	
	script.Parent.ValueLabel.Text = tostring(intVal.Value)
	
	region:SetAttribute("CustomGravity", intVal.Value)
end)

help is VERY much appreciated. chatgpt cant help me

still havent come up with any answers for this one :frowning:

Hey, it seems that your creating a new RunSerivce.Heartbeat which could be an issue because it’s making multiple RunSerivce.Heartbeat

I tweaked your script

-- LOCAL SCRIPT

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local regionPart = script.Parent:WaitForChild("RegionPart")
local defaultGravity = 196

local heartbeatConnection 

local function isPlayerInRegion(character)
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if not humanoidRootPart then return false end

	local region = Region3.new(
		regionPart.Position - regionPart.Size / 2,
		regionPart.Position + regionPart.Size / 2
	)

	local partsInRegion = workspace:FindPartsInRegion3(region, nil, math.huge)
	for _, part in pairs(partsInRegion) do
		if part:IsDescendantOf(character) then
			return true
		end
	end
	return false
end


local function updateGravity(character)
	local gravityValue = regionPart:GetAttribute("CustomGravity") or defaultGravity
	if isPlayerInRegion(character) then
		workspace.Gravity = gravityValue
	else
		workspace.Gravity = defaultGravity
	end
end


local function onCharacterAdded(character)
	character:WaitForChild("HumanoidRootPart")

	-- if there's an existing connection to heartbeat, disconnect it to avoid duplication !!!!
	if heartbeatConnection then
		heartbeatConnection:Disconnect()
	end

	heartbeatConnection = RunService.Heartbeat:Connect(function()
		updateGravity(character)
	end)
end

player.CharacterAdded:Connect(onCharacterAdded)

if player.Character then
	onCharacterAdded(player.Character)
end

regionPart:GetAttributeChangedSignal("CustomGravity"):Connect(function()
	if player.Character then
		updateGravity(player.Character)
	end
end)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.