Change value when connection with part is broken

I have a script that changes the gravity of only one person when they come into contact with an object of a specific name.

I tried to make it so when the player looses contact with that part, their custom gravity returns to 0.

I did try to program it myself but I couldn’t get it to work, I do believe it is very easy to make but I couldn’t find out how to make it work. In any case here’s the script!

wait()
local player = game.Players.LocalPlayer

game.Workspace.MoonGravity.Touched:Connect(function(hit)
	if hit == nil then return end
	if hit.Parent == nil then return end
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum ~= nil then
		if game.Players:GetPlayerFromCharacter(hum.Parent) == player then
			game.Workspace.Gravity = 30
		end
	end
end)

game.Workspace.NormalGravity.Touched:Connect(function(hit)
	if hit == nil then return end
	if hit.Parent == nil then return end
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum ~= nil then
		if game.Players:GetPlayerFromCharacter(hum.Parent) == player then
			game.Workspace.Gravity = 196.2
		end
	end
end)

I am thankful for any help!

Small edit: It’s a LocalScript inside of StarterCharacterScripts.

You can use touchEnded

Example:

game.Workspace.MoonGravity.Touched:Connect(function(hit)
	if hit == nil then return end
	if hit.Parent == nil then return end
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum ~= nil then
		if game.Players:GetPlayerFromCharacter(hum.Parent) == player then
			game.Workspace.Gravity = 30
		end
	end
end)

game.Workspace.MoonGravity.TouchEnded:Connect(function(hit)
	if hit == nil then return end
	if hit.Parent == nil then return end
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum ~= nil then
		if game.Players:GetPlayerFromCharacter(hum.Parent) == player then
			game.Workspace.Gravity = 196.2
		end
	end
end)
2 Likes

Also make sure to disconnect these touched events when they are no longer in use, as they will use memory.

I would recommend reading this for handling touched events:

NOTE:

if you are always going to be checking for someone touching this part then I don’t think its necessary for you to disconnect them, someone correct me if I’m wrong pls.

1 Like

I did try using the script and it does not appear to be working anymore. I did test it on a separate, empty world with no other scripts to make sure it’s not some kind of interference, but it doesn’t seem to be working still, do I have to put it in a different location than StarterCharacterScripts?

I gave you an example of how you would implement it into your system, I really should have said pseudo code, you have to make it work with your system yourself.

I understand, I will try to implement it myself but I do not know much about programming, I did take this script from the toolbox if I remember right, I did attempt to change it up to work with my game but I couldn’t do so with my level of knowledge. Still, I will try somehow.

1 Like

I decided to look for a different script as I noticed this one has a very weird bug where the gravity is seemingly slowly applied to other blocks, and it also makes other scripts in my game not function properly.

1 Like