Unintended behaviour in a script

I’ve been experiencing some issues with this script, basically, i want the player to die if he touches the water, currently, i use this script:

--DEV NOTE: Script is not optimized, fix it ASAP
 --Begin-- 
local Humanoid = script.Parent.Humanoid
while true do
	local Material = Humanoid.FloorMaterial 
	if Material == Enum.Material.CrackedLava or Humanoid:GetState() == Enum.HumanoidStateType.Swimming then
		Humanoid:TakeDamage(100)
	end
	wait(1/60) 
end

However, sometimes if the player quickly jumps on the water death is avoided, and i don’t find what causes it.

I’d do this:

--DEV NOTE: Script is not optimized, fix it ASAP
 --Begin-- 
local Humanoid = script.Parent.Humanoid
while true do
	local Material = Humanoid.FloorMaterial 
	if Material == Enum.Material.CrackedLava or Humanoid:GetState() == Enum.HumanoidStateType.Swimming or Material == Enum.Material.Water then
		Humanoid:TakeDamage(100)
	end
	wait(0.1) 
end

As then if water is touch they’ll actually die.

Tested it, it didn’t change anything.

what i think the problem is, is the default animation of jumping lifts the character up like a stud or two. When someone holds down space, the animation is constantly playing, which makes them float for a second and not actually touch the ground.

what you have to do is make the water part cancollide false so that the player can actually touch it and the jump animation doesnt get in the way

It is not possible to do that, it’s WaterMaterial, not a part.

oh alright then. nevermind lol

i guess what you could do is either place a part above, and/or add a jump cooldown

Cast a ray down with Terrain in the whitelist to check if the player is in water if the state isn’t “Swimming”. Also, you should connect these kinds of functions to Heartbeat.

How do i do that? I’ve never worked with rays.

local Part, position, normal,material = workspace:FindPartOnRayWithWhitelist(Ray.new(characterposition, -Vector3.new(0,2.5,0)), {Terrain})

EDIT: Whoops…

I’m not using PARTS, i’m using TerrainWater and as such, i’m not trying to look for PARTS, i’m looking for TerrainWater. Is there any way to check for TerrainWater?

FindPartOnRay also detects Terrain as it’s actually a BasePart Instance.

I’d try maybe using StateChanged to detect the water instead.
(firefox isnt letting me put tabs sorry)

local Humanoid = script.Parent.Humanoid
Humanoid.StateChanged:Connect(function(_,new)
if new == Enum.HumanoidStateType.Swimming then
Humanoid:TakeDamage(100)
end
end)
while true do
	local Material = Humanoid.FloorMaterial 
	if Material == Enum.Material.CrackedLava then
		Humanoid:TakeDamage(100)
	end
	wait(1/60) 
end

also if you want to make sure they die try making them takedamage for their maxhealth

1 Like

Answers


3 Likes