• !stuck command
    26 replies, posted
Hey there, I would like something that simply, when any player types !stuck in chat, it creates a 45 second timer, displays it on their screen such as "you will be respawned in: xx" and then respawns them. The player should only be allowed to use this when alive, and if they die after typing the command, the timer is destroyed. Thanks.
Bumpity bump. [highlight](User was banned for this post ("Bump" - mahalis))[/highlight]
be patient... MY FREIND (xomaxhox) IS doing it.... slowly....
Suuush, I was gonna give him the whole code at once but you spoiled my suprise :D Will edit back when I'm done
Oh awesome, good to hear. Thanks a lot man :) I look forward to it.
Isn't it easier to just type kill in console?
Not when you're playing a round-based gamemode and you spawn inside a wall, no. Or perhaps in prop hunt, where you turn into a prop and get stuck inside another prop.
[QUOTE=jimbodude;27127760]Or perhaps in prop hunt, where you turn into a prop and get stuck inside another prop.[/QUOTE] Wouldn't 45 seconds be a bit long then in prop hunt. Since you're going to be somewhat obvious prop clipping into another prop/wall
Eugh, I failed at this, sorry but It's not working (I'm not good at lua, just trying to help someone and then if I had a need for it I had it aswell Incase you wanted to even see my fail code and maybe someone fix it: [lua]local StuckDelay = 45 function Stuck( ply, text, team ) if (string.sub(text, 1, 5) == "!stuck") then return !ply:Alive() timer.Create( "StuckTimer", StuckDelay, 1, function( ply ) ply:Spawn() timer.Destroy( "StuckTimer" ) end ) end end hook.Add( "PlayerSay", "Stuck", Stuck ) --Draw the timer if (CLIENT) then function StuckTime() if timer.IsTimer( "StuckTimer" ) then draw.DrawText( ("Time Left Until Respawn: "..math.Round(StuckDelay-CurTime())), "HUDNumber4", ScrW() / 2, ScrH() / 2, Color(255,140,0,255), 1 ) end end hook.Add( "HUDPaint", "StuckTime", StuckTime ) end[/lua]
[url]http://wiki.garrysmod.com/?title=Gamemode.PlayerDeathThink[/url] look at the example, change it to 45 seconds, fit it in with the Stuck function xomaxhox made, and edit line 5 where it says "return !ply:Alive()" to "ply:Kill()", inside the Stuck function do "ply.suicide = 1" and in the wiki example add "&& ply.suicide = 1" to "CurTime()>ply.nextsecond" but that doesn't stop them from typing in "suicide" within the console.
[QUOTE=Zephilinox;27146389][url]http://wiki.garrysmod.com/?title=Gamemode.PlayerDeathThink[/url] but that doesn't stop them from typing in "suicide" within the console.[/QUOTE] [url=http://wiki.garrysmod.com/?title=Gamemode.CanPlayerSuicide]This[/url] will though :D And also thanks for fixing my code, I had no idea what was left to do to fix it :L
[QUOTE=xomaxhox;27130024]Eugh, I failed at this, sorry but It's not working (I'm not good at lua, just trying to help someone and then if I had a need for it I had it aswell Incase you wanted to even see my fail code and maybe someone fix it: [lua]local StuckDelay = 45 function Stuck( ply, text, team ) if (string.sub(text, 1, 5) == "!stuck") then return !ply:Alive() timer.Create( "StuckTimer", StuckDelay, 1, function( ply ) ply:Spawn() timer.Destroy( "StuckTimer" ) end ) end end hook.Add( "PlayerSay", "Stuck", Stuck ) --Draw the timer if (CLIENT) then function StuckTime() if timer.IsTimer( "StuckTimer" ) then draw.DrawText( ("Time Left Until Respawn: "..math.Round(StuckDelay-CurTime())), "HUDNumber4", ScrW() / 2, ScrH() / 2, Color(255,140,0,255), 1 ) end end hook.Add( "HUDPaint", "StuckTime", StuckTime ) end[/lua][/QUOTE] [lua]if (string.sub(text, 1, 5) == "!stuck") then[/lua] should be [lua]if (string.sub(text, 1, 6) == "!stuck") then[/lua] it reads the first 6 characters of the string, and our word is 6 characters, not 5.
[QUOTE=c-unit;27152672][lua]if (string.sub(text, 1, 5) == "!stuck") then[/lua] should be [lua]if (string.sub(text, 1, 6) == "!stuck") then[/lua] it reads the first 6 characters of the string, and our word is 6 characters, not 5.[/QUOTE] Please excuse my retardedness :D [b]EDIT[/b] Also for the simplicity of others I put the code into one for you [lua]maxdeathtime = 10 function Stuck( ply, text, team ) if (string.sub(text, 1, 6) == "!stuck") then ply:Kill() ply.suicide = 1 end end hook.Add( "PlayerSay", "Stuck", Stuck ) function Stuck_InitDeath( ply, wep, killer ) ply.deathtime = 0 ply.nextsecond = CurTime() + 1 end hook.Add( "PlayerDeath", "Stuck_InitDeath", Stuck_InitDeath ) function StuckForceRespawn( ply ) if (CurTime()>ply.nextsecond and ply.suicide == 1) then if (ply.deathtime < maxdeathtime) then ply.deathtime = ply.deathtime + 1 ply.nextsecond = CurTime()+1 else ply.deathtime = nil ply.nextsecond = nil ply:Spawn() end end end hook.Add( "PlayerDeathThink", "StuckForceRespawn", StuckForceRespawn ) --Draw the timer function StuckTime() end hook.Add( "HUDPaint", "StuckTime", StuckTime )[/lua] But can someone make the HUD work for it please, it is supposed to draw text in the middle of the players screen saying how much time is left until respawn. I've tried and failed a LOT :D
You are trying to use HUDPaint server sided, use an if SERVER else check.
I was actually running it Shared And also when I tried doing: [lua]--Draw the timer if CLIENT then function StuckTime() if LocalPlayer().Suicide == 1 then draw.DrawText( ("Time Left Until Respawn: " .. LocalPlayer().NextSecond), "HUDNumber4", ScrW() / 2, ScrH() / 2, Color(255,140,0,255), TEXT_ALIGN_CENTER ) end end hook.Add( "HUDPaint", "StuckTime", StuckTime ) end[/lua] But it wasn't working
Because you have to usermessage the value to the client about the countdown starting, clients and the server don't automatically share every updated code available. [editline]3rd January 2011[/editline] [lua] if SERVER then local maxdeathtime = 10 function Stuck( ply, text, team ) if (string.sub(text, 1, 6) == "!stuck") then if ply:Alive() then ply:Kill() umsg.Start("StuckCountdownStarted", ply) umsg.Short(CurTime() + maxdeathtime) umsg.End() ply.NextRespawn = CurTime() + maxdeathtime end end end hook.Add( "PlayerSay", "Stuck", Stuck ) function StuckForceRespawn( ply ) if ply.NextRespawn and ply.NextRespawn > CurTime() then return true end -- Return to keep them from respawning if ply.NextRespawn then ply.NextRespawn = nil ply:Spawn() end end hook.Add( "PlayerDeathThink", "StuckForceRespawn", StuckForceRespawn ) end if CLIENT then --Draw the timer function StuckTime() if RespawnCountDownStarted then draw.DrawText( ("Time Left Until Respawn: " .. tostring(math.Round(RespawnCountDownStarted-CurTime()))), "HUDNumber4", ScrW() / 2, ScrH() / 2, Color(255,140,0,255), TEXT_ALIGN_CENTER ) if CurTime() > RespawnCountDownStarted then RespawnCountDownStarted = nil end end end hook.Add( "HUDPaint", "StuckTime", StuckTime ) function StuckCountdownStarted(um) RespawnCountDownStarted = um:ReadShort() end usermessage.Hook("StuckCountdownStarted", StuckCountdownStarted) end [/lua] That's a working code (I think), also removed some unneeded code since there is no point of having it there.
I tried using this, however after typing stuck, the player is not respawned after 10 seconds, nor is the HUD message displayed. (Ignore chat.AddTexT() being client sided.) [lua] if SERVER then AddCSLuaFile("stuck.lua") local maxdeathtime = 10 function Stuck( ply, text, team ) if ply:Team() == TEAM_SPECTATOR then return end if ply:Team() == TEAM_SPEC then return end if (string.sub(text, 1, 6) == "!stuck") then if ply:GetNWInt("respawn") >= 1 then chat.AddText(ply,Color(255,255,0),"[STUCK] ",Color(0,255,255),"You have used your 1 respawn this life.") else if ply:Alive() then for k, v in pairs(player.GetAll()) do chat.AddText(v,Color(255,255,0),"[STUCK] ",Color(0,255,0),ply:Nick(),Color(0,255,255)," is stuck and has requested to be respawned!") end umsg.Start("StuckCountdownStarted", ply) umsg.Short(CurTime() + maxdeathtime) umsg.End() ply.NextRespawn = CurTime() + maxdeathtime ply:SetNWInt("respawn", ply:GetNWInt("respawn")+1) end end end end hook.Add( "PlayerSay", "Stuck", Stuck ) function StuckForceRespawn( ply ) if ply.NextRespawn and ply.NextRespawn > CurTime() then return end -- Return to keep them from respawning if ply.NextRespawn then if ply:Alive() then ply.NextRespawn = nil ply:Spawn() for k, v in pairs(player.GetAll()) do chat.AddText(v,Color(255,255,0),"[STUCK] ",Color(0,255,0),ply:Nick(),Color(0,255,255)," got respawned!") end -- ply:SetNWInt("respawn", ply:GetNWInt("respawn")+1) end end end hook.Add( "PlayerDeathThink", "StuckForceRespawn", StuckForceRespawn ) local function ResetRespawn(ply) ply:SetNWInt("respawn", 0) end hook.Add( "PlayerSpawn","PlayerSpawnResetRespawn",ResetRespawn ) end if CLIENT then --Draw the timer function StuckTime() if LocalPlayer():Alive() then if RespawnCountDownStarted then draw.DrawText( ("Time Left Until Respawn: " .. tostring(RespawnCountDownStarted-CurTime())), "HUDNumber4", ScrW() / 2, ScrH() / 2, Color(255,140,0,255), TEXT_ALIGN_CENTER ) if CurTime() > RespawnCountDownStarted then RespawnCountDownStarted = nil end end end end hook.Add( "HUDPaint", "StuckTime", StuckTime ) function StuckCountdownStarted(um) RespawnCountDownStarted = um:ReadShort() end usermessage.hook("StuckCountdownStarted", StuckCountdownStarted) end[/lua]
Oh sorry, I had some spelling errors in my code usermessage.hook should be usermessage.Hook in the playerdeaththink, return true, not just return
I'm now using: [lua] if SERVER then AddCSLuaFile("stuck.lua") local maxdeathtime = 10 function Stuck( ply, text, team ) if ply:Team() == TEAM_SPECTATOR then return end if ply:Team() == TEAM_SPEC then return end if (string.sub(text, 1, 6) == "!stuck") then if ply:GetNWInt("respawn") >= 1 then chat.AddText(ply,Color(255,255,0),"[STUCK] ",Color(0,255,255),"You have used your 1 respawn this life.") else if ply:Alive() then for k, v in pairs(player.GetAll()) do chat.AddText(v,Color(255,255,0),"[STUCK] ",Color(0,255,0),ply:Nick(),Color(0,255,255)," is stuck and has requested to be respawned!") end umsg.Start("StuckCountdownStarted", ply) umsg.Short(CurTime() + maxdeathtime) umsg.End() ply.NextRespawn = CurTime() + maxdeathtime ply:SetNWInt("respawn", ply:GetNWInt("respawn")+1) end end end end hook.Add( "PlayerSay", "Stuck", Stuck ) function StuckForceRespawn( ply ) if ply.NextRespawn and ply.NextRespawn > CurTime() then return true end -- Return to keep them from respawning if ply.NextRespawn then if ply:Alive() then ply.NextRespawn = nil ply:Spawn() for k, v in pairs(player.GetAll()) do chat.AddText(v,Color(255,255,0),"[STUCK] ",Color(0,255,0),ply:Nick(),Color(0,255,255)," got respawned!") end -- ply:SetNWInt("respawn", ply:GetNWInt("respawn")+1) end end end hook.Add( "PlayerDeathThink", "StuckForceRespawn", StuckForceRespawn ) local function ResetRespawn(ply) ply:SetNWInt("respawn", 0) end hook.Add( "PlayerSpawn","PlayerSpawnResetRespawn",ResetRespawn ) end if CLIENT then --Draw the timer function StuckTime() if LocalPlayer():Alive() then if RespawnCountDownStarted then draw.DrawText( ("Time Left Until Respawn: " .. tostring(RespawnCountDownStarted-CurTime())), "HUDNumber4", ScrW() / 2, ScrH() / 2, Color(255,140,0,255), TEXT_ALIGN_CENTER ) if CurTime() > RespawnCountDownStarted then RespawnCountDownStarted = nil end end end end hook.Add( "HUDPaint", "StuckTime", StuckTime ) function StuckCountdownStarted(um) RespawnCountDownStarted = um:ReadShort() end usermessage.Hook("StuckCountdownStarted", StuckCountdownStarted) end[/lua] Now, the timer displays just fine, but when it hits 0, nothing happens. No errors are reported.
My example works fine for me when running it both server and client. Are you sure that the server includes the file as well as the client?
Hmm, yes, as it's in autorun/server. I changed your example up a bit, to include some chat prints, and not to kill the player, just respawn them, ie, teleport to new spawn point. I also added an NWInt "respawn", which only gives players one respawn for each time the "PlayerSpawn" hook is called. If you wouldn't mind giving my edits a quick once over, I'd greatly appreciate it, as I'm sure it's something very silly.
I don't think PlayerDeathThink is even called if the player is alive.. I think that's your problem. Also, I would suggest just storing a table entry for the player as respawn, instead of nwvars, you have no point of sending it to the client.
Right, of course, yeah. So it'd be best to simply hook that to "Think" ?
If you have to have the player alive, I suggest to do the respawn in a timer actually, even though I said they would be horrible for the job before. Do a timer.Simple(maxdeathtime, function(ply) ply:Spawn() end, ply) and remove the NextSpawn player var, and remove the deaththink function.
[lua]if SERVER then AddCSLuaFile("suck.lua") hook.Add("PlayerSay", "Stuck", function( ply ) if (string.sub(text, 1, 6) == "!stuck") then ply:SetNetworkedString("AmIStck", "ImStuck") end end) else if !LocalPlayer():GetNetworkedString("AmIStuck") == "ImStuck" and !LocalPlayer().StuckTime then LocalPlayer().StuckTime = CurTime() elseif LocalPlayer():GetNetworkedString("AmIStuck") == "No" then LocalPlayer().StuckTime = nil end if LocalPlayer().StuckTime and LocalPlayer():GetNetworkedString("AmIStuck") == "ImStuck" and (CurTime() - LocalPlayer().StuckTime) < 15 then local timeleft = math.Round(math.Clamp(15 - (CurTime() - LocalPlayer().StuckTime), 0, 15)) draw.SimpleText(timeleft.." seconds until you respawn.", "ScoreboardText", Scrw()/2, ScrH()/2 - 25, Color(255, 0, 0, 255)) elseif (CurTime() - LocalPlayer().StuckTime) < 0 then LocalPlayer().StuckTime = nil LocalPlayer():GetNetworkedString("AmIStuck") == "No" end end [/lua] Just incase you were still looking for this, kinda shitty and not sure if it works I just got bored so I thought I would post this, anyways make a file called stuck.lua in lua/autorun. Tell me if it works.
Thanks for that. Currently I'm using this on my server and it seems to be doing the job nicely: [lua] if SERVER then AddCSLuaFile("stuck.lua") local maxdeathtime = math.Rand(8,20) function Stuck( ply, text, team ) if ply:Team() == TEAM_SPECTATOR then return end if ply:Team() == TEAM_SPEC then return end if (string.sub(text, 1, 6) == "!stuck") then if ply:GetNWInt("respawn") > 0 then chat.AddText(ply,Color(150,100,255),"[STUCK] ",Color(0,255,255),"You have used your 1 respawn this round.") else if ply:Alive() then if ply:GetNWInt("respawn") <= 1 then for k, v in pairs(player.GetAll()) do chat.AddText(v,Color(150,100,255),"[STUCK] ",Color(0,255,0),ply:Nick(),Color(0,255,255)," is stuck and has requested to be respawned!") end end umsg.Start("StuckCountdownStarted", ply) umsg.Short(CurTime() + maxdeathtime) umsg.End() ply:SetNWInt("respawn", ply:GetNWInt("respawn")+1) timer.Simple(maxdeathtime, function(ply) if ply:GetNWInt("respawn") <= 1 then if ply:Alive() then ply:Spawn() for k, v in pairs(player.GetAll()) do chat.AddText(v,Color(150,100,255),"[STUCK] ",Color(0,255,0),ply:Nick(),Color(0,255,255)," got respawned!") end else for k, v in pairs(player.GetAll()) do chat.AddText(v,Color(150,100,255),"[STUCK] ",Color(0,255,0),ply:Nick(),Color(0,255,255)," died before getting the chance to respawn!") end end end end,ply) else chat.AddText(ply,Color(150,100,255),"[STUCK] ",Color(0,255,255),"You can only use !stuck when alive!") end end end end hook.Add( "PlayerSay", "Stuck", Stuck ) local function ResetRespawn(ply) ply:SetNWInt("respawn", 0) end local function ResetRespawny(ply) ply:SetNWInt("respawn", 0) end hook.Add( "OnRoundStart","RoundResetRespawn",ResetRespawny ) hook.Add( "OnRoundEnd","RoundResetRespawny",ResetRespawn ) end if CLIENT then function StuckTime() if LocalPlayer():Alive() then if RespawnCountDownStarted then draw.DrawText( ("Time Left Until Respawn: " .. tostring(math.Round(RespawnCountDownStarted-CurTime()))), "HUDNumber4", ScrW() / 2, ScrH() / 2, Color(255,140,0,255), TEXT_ALIGN_CENTER ) if CurTime() > RespawnCountDownStarted then RespawnCountDownStarted = nil end end end end hook.Add( "HUDPaint", "StuckTime", StuckTime ) function StuckCountdownStarted(um) RespawnCountDownStarted = um:ReadShort() end usermessage.Hook("StuckCountdownStarted", StuckCountdownStarted) end [/lua]
[QUOTE=jimbodude;27127760]Not when you're playing a round-based gamemode and you spawn inside a wall, no. Or perhaps in prop hunt, where you turn into a prop and get stuck inside another prop.[/QUOTE] Bind K kill then press k to suicide all the time
Sorry, you need to Log In to post a reply to this thread.