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

Help with oxygen script?

game.Workspace.WATER.Touched:connect(function(hit)
wait(2)
	hit = game.Players:GetPlayerFromCharacter(hit.Parent)
		game.Players:FindFirstChild(hit).Oxygen.Value = game.Players:FindFirstChild(hit).Oxygen.Value - 2
		
end)

Basically the player has a numbervalue called oxygen and I want to make it so while the player is touching water the value has 2 subtracted every 2 seconds but this script doesn't work the error I get is 16:34:25.714 - Workspace.WATER.Script:3: attempt to index a nil value

1 comment

0

name 'hit' something else, you have 2 things named 'hit'

1 answer

Your "hit" variable was already classified by the event. Basically, you didn't need to set a hit variable after the player touches the "WATER", as the event already found the part that hit it.

game.Workspace.WATER.Touched:connect(function(hit)
wait(2)
	local player = hit.Parent
	player.Oxygen.Value = player.Oxygen.Value - 2
end)

Pretty sure that should work

2 comments

0

Now when I hit it it says "17:15:12.023 - Oxygen is not a valid member of Model"

0

Oh, my bad! hit.Parent is the players character. You'll need to use the "Get player from Character" method of hit.Parent, something like "local player = game.Players:GetPlayerFromCharacter(hit.Parent)" , sorry

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