Auto-Sleep script help

Hello.

I’m not going to get too much into detail, but I am making a script within the DarkRP sleep module that automatically toggles the sleep function if your health is under 30. It works but I get an error when the person asleep is shot: “Alive” is a nil value.

The error is from the default darkrp sleep module where it says: “if player:Alive() then”
I don’t see why it breaks the default sleep module code when all I’m doing is toggling it. I was going to remove the chat commands too so I can’t make it run the chat command.

Here’s my script within darkrp/gamemode/modules/sleep


 function detectHealth( ply )
          if ply:Health() <= 30 then
          DarkRP.ToggleSleep(ply, "force") -- this might be wrong as I'm on mobile but in the actual script it's right.
         end
end 

I’d recommend looking into meta-tables. Tabs and spaces / ie code formatting is for us, the human element, to easily and quickly identify / read code. I’d recommend looking into a code standard and following it, or at least tab in when you type a function, for, if, etc… and tab out on end, etc…

I don’t see where you’re calling Alive. It may be possible where you’re calling it that the player is invalid; do an IsValid check first.

Are you using 2.4.3 or 2.5?

Tabbing demo:


function detectHealth( ply )
	if ply:Health() <= 30 then
		DarkRP.ToggleSleep(ply, "force") -- this might be wrong as I'm on mobile but in the actual script it's right.
	end
end

Using meta-tables and enumeration / CONSTs:


local SLEEP_HEALTH_THRESHOLD = 30;
local META_PLAYER = FindMetaTable( "Player" );

// Create useful / meaningful functions; this will return true or false depending on the health the user has reached the health Threshold
function META_PLAYER:ReachedSleepHealthThreshold( )
	return ( self:Health() <= SLEEP_HEALTH_THRESHOLD );
end

Without seeing the full code, we really can do more than speculate.

Hey, welcome to FacePunch. This forum is for devs that need help working on things. Here are some resources to help you get started:

General Learning Lua Help Thread ( Contains links to helpful topics, and websites ): http://forum.facepunch.com/showthread.php?t=1337945

Problems that don’t need their own thread ( All simple questions / quickies should go here, but please do not post a question here AND make a new topic… ): http://forum.facepunch.com/showthread.php?t=1411111

OLD Problems and Solutions thread ( contains a massive amount of useful data ): http://forum.facepunch.com/showthread.php?t=1348923

Garry’s Mod Wiki: http://wiki.garrysmod.com/page/Main_Page

Old Garry’s Mod Wiki ( Contains a lot of useful information; some content may not work without modification within GM13 ): http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index4875.html

DarkRP Help Thread ( ALL DarkRP Questions go here ): http://forum.facepunch.com/showthread.php?t=1249475

DarkRP Wiki ( Most questions can be answered here ): http://wiki.darkrp.com/index.php/Main_Page

Creating Jobs in DarkRP: http://wiki.darkrp.com/index.php/DarkRP:CustomJobFields

Creating Shipments in DarkRP: http://wiki.darkrp.com/index.php/DarkRP:CustomShipmentFields

Thank you for your help ^^ I’ll try that as soon as I wake up tomorrow. I’m using darkrp 2.5 btw
The reason you can’t see alive being called is because i didn’t include it in the example. If you want to look at the sleep module code just download darkrp and go into the sleep module. It’s In by default. I think line 22 is where the alive check is I think. Anyway I’m looking into another method if that doesn’t work. One that spawns a ragdoll and black screens the player. Problem is, I can’t work out how to detect damage on the players unconscious ragdoll and take away their health whilst down. If you could help with that I’d be grateful.

Thanks and have a nice day :slight_smile:

Look at how DarkRP does it:



local function DamageSleepers(ent, dmginfo)
	local inflictor = dmginfo:GetInflictor()
	local attacker = dmginfo:GetAttacker()
	local amount = dmginfo:GetDamage()

	local ownerint = ent.OwnerINT
	if ownerint and ownerint ~= 0 then
		for k,v in pairs(player.GetAll()) do
			if v:EntIndex() == ownerint then
				if attacker == game.GetWorld() then
					amount = 10
					dmginfo:ScaleDamage(0.1)
				end
				v:SetHealth(v:Health() - amount)
				if v:Health() <= 0 and v:Alive() then
					DarkRP.toggleSleep(v, "force")
					 -- reapply damage to properly kill the player
					 v:StripWeapons()
					v:TakeDamageInfo(dmginfo)
				end
			end
		end
	end
end
hook.Add("EntityTakeDamage", "Sleepdamage", DamageSleepers)


Yeah I tried. It gives no error but it doesn’t work either. I spent a while today trying to do it but for some reason it just won’t kill the damn player.

Why don’t you build off of that code to achieve what you want to do? Instead of forcing the player to ‘sleep’ at 0 health, change it to the 30 you wanted? So:



local function DamageSleepers(ent, dmginfo)
	local inflictor = dmginfo:GetInflictor()
	local attacker = dmginfo:GetAttacker()
	local amount = dmginfo:GetDamage()

	local ownerint = ent.OwnerINT
	if ownerint and ownerint ~= 0 then
		for k,v in pairs(player.GetAll()) do
			if v:EntIndex() == ownerint then
				if attacker == game.GetWorld() then
					amount = 10
					dmginfo:ScaleDamage(0.1)
				end
				v:SetHealth(v:Health() - amount)
				if v:Health() <= 30 and v:Alive() then
					DarkRP.toggleSleep(v, "force")
					 -- reapply damage to properly kill the player
					 v:StripWeapons()
					v:TakeDamageInfo(dmginfo)
				end
			end
		end
	end
end
hook.Add("EntityTakeDamage", "Sleepdamage", DamageSleepers)


Not sure if that will work though. Perhaps show us what you have now?

That might not work. I can give you a rough example of what I have since I’m not on my computer. I can’t use the sleep module as it’s buggy but as you said, just build off code.


 function Unconscious( ply )

local ragdoll = ent("prop_ragdoll") -- this is probably wrong. Without my actual script I'm just guessing it was this


ragdoll:SetPos(ply:GetPos)
ragdoll:Model(ply:Team)
-- I can't remember the rest of the code but you get the idea
end

function TakeUDamage(ply)

-- here is where the damage code would go


-- the. I would create a death ragdoll that can't be revived. I managed to get the sleeping pills to knock you unconscious too. All that's left is to inflict damage when players unconscious ragdoll is shot and make it so it spawns the dead ragdoll