This is annoying as fuck, to put it nicely.
I've tried a lot of things such as using SetHealth, changing run speed, etc... But none work. So, I'm going to try it with GodEnable and GodDisable because it's the only one that works so far, kinda.
I'm just doing this to see how timers work and to learn more about them, but I want to use ply/LocalPlayer which I can't unless I use a k,v pairs check, which is stupid. I also don't have any friends so, I can't have anyone check it for me, but I ASSUME that this'll turn off godmode for all players. So, obviously that's not what I want to do because someone can just die over and over and it'll disable godmode for all players.
[LUA]
TimersAreMean = true
GEnable = false
function MyShittyFunction(ply)
if ply:Alive() and (TimersAreMean == true) then
GEnable = true
ply:GodEnable()
end
end
hook.Add("PlayerSpawn","blahh",MyShittyFunction)
function TimersHatePly(ply)
if (GEnable == true) then
ply:GodDisable()
end
end
timer.Create("AVerySpecialTimerWithAutism", 10, 1, TimersHatePly)
[/LUA]
Here's what I'm trying to make:
1) Player dies
2) On respawn it'll enable godmode for the player
3) 10 seconds later it'll disable godmode
It sounds simple and it should be, but it isn't because apparently "<shittycode> attempt to index local 'ply' <a nil value>"
I've defined ply in the function as local and non-local and I've also tried to define it outside of the function, but it won't work.
It'll just say LocalPlayer is a nil value instead of ply... something like that.
It doesn't really matter "what" it is, it could be the run speed, player color, etc. I just want to see how it works and how to implement this, but it doesn't.
Is there a way to do this? It seems that timers hate ply/LocalPlayer. It works when it's a k,v in pairs check through all players. Also, this isn't my first shitty code, I've tried a lot of different ways, this is just the most recent one I did.
Timer functions don't hold any player information.
To be able to use a player entity within a timer, you must call the timer inside a hook / function that actually has the player entity called.
[lua]
GEnable = false
function MyShittyFunction( ply )
ply:GodEnable()
GEnable = true
timer.Create("AVerySpecialTimerWithAutism", 10, 1, function()
if GEnable and IsValid(ply) then
ply:GodDisable()
GEnable = false
end
end)
end
hook.Add("PlayerSpawn", "blahh", MyShittyFunction)
[/lua]
Be aware that the timer will be reset for each time a player spawns, because the timer is being recreated every time. I'm not sure what the point of the GEnable was, but I left it in there regardless.
That's not going to work since you're making a timer with the exact same name for every player who spawns because it's serverside not clientside.
[lua]
local GodMode = {
Settings = {
Length = 3
}
}
function GodMode:PlayerSpawn(ply)
if !IsValid(ply) then return end
ply:GodEnable()
ply.GodEnabled = true
timer.Simple(self.Settings.Length, function()
if !IsValid(ply) then return end
ply:GodDisable()
ply.GodEnabled = false
end)
end
hook.Add("PlayerSpawn", "GE.PlayerSpawn", function(...) GodMode:PlayerSpawn(...) end)
[/lua]
When your timer runs it calls your function with no parameters. So, it is calling TimersHatePly() which is the same as TimersHatePly(nil), so yes, ply is nil. You need to wrap it inside another function that passes your parameters for you.
When a player spawns this calls GodEnable and starts a timer for that player.
[lua]
local function UngodPlayer(ply)
if IsValid(ply) then --Make sure the player is still valid (they might've left the game by now, etc)
ply:GodDisable()
end
end
local function PlayerSpawned(ply)
ply:GodEnable()
timer.Create("Ungod Player "..ply:EntIndex(), 10, 1, function() --Create a unique timer for this player
UngodPlayer(ply) --Call your function and pass ply along
end)
end
hook.Add("PlayerSpawn", "My Unique Spawn Hook", ply)
[/lua]
EDIT: Ninja'd, brandon did basically the same thing :)
[QUOTE=Shinycow;43005014]~~Orgasmic stuff~~[/QUOTE]
Yeah, I left GEnable in there as a variable to check when they were seperate... Well, something like that at least.
I never even considered that was an issue, I was looking for a complicated answer to what was wrong. The solution is pretty simple and less coding, too.
[del]It works perfectly, thanks for the help![/del]
[editline]27th November 2013[/editline]
Wait, what?
Can you elaborate, Brandonj4?
It "seems" like none of the codes are working. Keep in mind I'm using them ALONE... I'm not adding onto my shitty code. So, I assume they're "stand alone". I'm kind of confused now because they're not creating any errors. The only thing that happens is at spawn the health doesn't go down for about 2 seconds, which is probably default on Garry's Mod at spawn.
[editline]28th November 2013[/editline]
Okay, well... Here's an "update" if you want to call it that.
Yes, ALL of the codes work. I just had to restart my server and it was fine. They were all tested in init.lua.
So, all work the same aside from Brandonj4's because the time was set for 3 so I mistakenly thought it didn't work because I was waiting for it to turn off at 10 seconds, but it works.
So, thanks everyone. I guess it's just personal preference? I don't see how one is better than another. I assume that FlameCow's is the best method because it's simple and does the job. Yes? No?
Br[b]a[/b]ndons code is the best one.
[QUOTE=brandonj4;43005020]That's not going to work since you're making a timer with the exact same name for every player who spawns because it's serverside not clientside.
[/QUOTE]
No shit, that's why I said that in the bottom of my post. I only did it that way to fix his code, not write completely different code for him.
[QUOTE=Shinycow;43005416]No shit, that's why I said that in the bottom of my post. I only did it that way to fix his code, not write completely different code for him.[/QUOTE]
Then stop giving people code that doesn't work.
Sorry, you need to Log In to post a reply to this thread.