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