Can i have someone code me a small script please

Hello. I’ve been trying to figure out how to make it so if a player goes into water it kills them. Could someone please code me something like that?

1 Like

That is pretty simple tbh

Using the IsPedSwimming native you can find out when the player is in the water in a while loop, at which point you kill them with SetEntityHealth.


client-sided

local ped = PlayerPedId()
while true do
    Wait(500)
    if IsPedSwimming(ped) and not IsEntityDead(ped) then -- Checks if player is swimming and is not dead already.
        SetEntityHealth(ped, 0) -- Drains players health.
    end
end

This should work, though I wont promise anything due to the fact that I cannot test it right now.

Would that be client or server?

I have no idea if this works as wanted, but this native exist SetPedDiesInstantlyInWater - Natives @ Cfx.re Docs.

Hi there Mayhem,

Anytime you’re checking on a client for something, it’s definitely going to be client-sided. If you needed information like this on the server-side, you would need to trigger a server event from the client side and pass the information.

I just tested it and it didn’t work.

1 Like

Just needed to be put in a Citizen thread. Tested to work as you needed.
waterdeath.zip (757 Bytes)

Thank you.

i just tested it and everytime someone died you would haft to restart the script. Is there a way where you could fix that?

I just tested and entering the water continues to kill me, no restart needed.

For me it will only kill me once

I’d say there’s some shenanigans afoot on your end of things then. I’d throw in some print statements to ensure that the loop is continuing for you as well as checking whether it considers you alive after respawn.

1 Like

I have no clue how to do that

Well, it’s a fantastic time to learn. You’re going to enjoy working on a server more if you don’t have to keep asking others to do everything for you. Let’s take the resource that vacexgaming provided you as an example:

Citizen.CreateThread(function()
	local ped = PlayerPedId()
	
	while true do
		Citizen.Wait(500)
		if IsPedSwimming(ped) and not IsEntityDead(ped) then -- Checks if player is swimming and is not dead already.
			SetEntityHealth(ped, 0) -- Drains players health.
		end
	end
end)

This is a simple loop that occurs every 500 milliseconds(or half a second). while true do just tells the loop to keep happening for as long as the resource is running. Unless something breaks it, it can’t stop/won’t stop. Now you’re inside the main loop and the script is checking to see if the player is swimming and not dead. If those two conditions are matched, it set’s the player’s health to 0. The ends of the statements are just closing the loops and if statements. This has to be done to keep from breaking the code.

The things I’d want to check is whether the script thinks I’m alive or not and whether I’m swimming or not. This would help me make sure that the script is registering my status correctly after the first death. An if/else statement would do that. Read it out loud just like you’re reading a book; "If Bob is swimming, yell “he’s swimming”, if he’s dead, yell “he’s dead”. Etc.

Citizen.CreateThread(function()
	local ped = PlayerPedId()
	
	while true do
		Citizen.Wait(500)
		if IsPedSwimming(ped) then -- Checks if player is swimming and is not dead already.
			if not IsEntityDead(ped) then
				SetEntityHealth(ped, 0) -- Drains players health.
				print('killing the ped.')
			else
				print('ped is already dead.')
			end
		else
			print('ped not swimming')
		end
	end
end)

Since this is a client-sided file, these print statements will end up in your F8 panel and CFX log files on your game computer. If you were doing print statements on a server-sided file, the print statements would end up on your server terminal output.

One thing to note is that this spams your log every half a second. You may want to slow the loop just for testing purposes, perhaps to every two seconds. That would mean you’d need to be in the water for 2 seconds before the script would register it during the loop but you can always lower it back down once you’re done testing. 2 seconds is 2000ms. I’ll let you figure out what to change to make that happen.

1 Like

I am - entry level but I would defiantly recommend an if statement to check the players location, before you boldly kill them, as there are pools to keep in mind :sweat_smile: I recommend this for that check.

or

The Thing is, I don’t have a death script in the server. Would that affect it?

The native will work, regardless of your scripts. Let me know what the print statements show both when you hit the water the first time as well as the second.