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

Best approach to swim/diving oxygen system?

Pretty basically I am new, been reading helpful posts for a while now, and using some tutorial/internet found code retyped and tailored to my needs. (taking several radios apart to see how they work situation.) I have been studying and reading as much as I can for a while now and messing around mostly, but recently focused on a project. So the particular problem, I was using an advised method of making a non-collidable/invisible part around my water area to deplete the intval in the leaderstat by an touch function. It touches several times due to player motion, depleting number by multiples, disregarding the wait. Code has a debounce thing I read should be put in, and I use datastore2, that is why the "Increment" and this is server side script to a remote function. But doing it this way seems to me like it would put more activity on server. After this code block, I explain more.

function onTouched(playerwhoclicked)
	
	local OnTouched = false
	local player = game:GetService("Players"):GetPlayerFromCharacter(playerwhoclicked.Parent)
	if player == nil then return end
wait(.1)
		for k, player in pairs(game.Players:GetChildren()) do
		local currentoxygenStore = DataStore2("CurrentOxygen", player)
			  currentoxygenStore:Increment(-1)
				if not OnTouched then
			OnTouched = true
			end
		OnTouched = false
	game.ReplicatedStorage.RemoteEvents.DiveCountStart:FireClient(player)

	end
end
workspace.Location.DiveBarrier.Touched:Connect(onTouched)

(yes, its a mess. I am learning. Not sure if I put the Debounce parts in right at all, but none of this throws errors, just multiple touches any time character moves.)

So thinking about client/server balance, my server does not need to see the countdown or track player head locations, just supply the players oxygen value. Client side could track the player and the water level, I put a boolean for divestatus into leaderstats table (leaderboard is shut off visually). My plan is when a characters head goes below a vector3int (water surface) in a workspace folder, this sends a remote event to get "maxoxygen" value from server, send value back to client with basically a countdown script to 0 at -1 per second, if the character's head goes back above vector3int, countdown stops, oxygen replenishes or oxygen hits 0 and they die. I tried change the divestatus boolean using the code above by changing currentoxygenStore and Current Oxygen to its values and changing the increment line to divestatusStore = true, did nothing. Have not had much luck finding ways to track players, I read a vector3int in a bodypart could work, but dont quite understand ins and outs of all of it, or if a scripts that does that should be in workspace, starterplayerscripts or what.

My question(s): I am sort of at a choice, and if I keep working at both, I will accomplish neither. Could the above ontouch script be re-worked so it could do a proper countdown easily, or would the 2nd idea be easy enough to accomplish with more understanding? And would the 2nd method be easier on the server?

Any suggestions on reading material focused on any of these topics is greatly appreciated as well.

1 answer

It's a complex script, so sadly I can't give you everything you need. Consider this a hint.

https://developer.roblox.com/en-us/api-reference/datatype/Region3 https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3

Use FindPartsInRegion3 after making a Region3 to see if the player's head is underwater. If it is, subtract oxygen.

1 comment

0

Thank you! I will check this out today and see what I can get working.

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