Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with oxygen script?

Asked by 8 years ago
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

0
name 'hit' something else, you have 2 things named 'hit' lucas4114 607 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

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

0
Now when I hit it it says "17:15:12.023 - Oxygen is not a valid member of Model" CybeTech 37 — 8y
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 konichiwah1338 15 — 8y
Ad

Answer this question