• Print

Author Topic: PointShop Respawn (for TTT)  (Read 8399 times)

0 Members and 1 Guest are viewing this topic.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
PointShop Respawn (for TTT)
« on: July 21, 2014, 03:29:02 pm »
I tried to do this on my own, and I did as much as I could, but I'm still very new to Lua and don't really know much

I am trying to create a "product" in the PointShop that will respawn a player. I took some of the code from a TTT respawn command I got in a pack of other TTT commands

Here's what I have right now
Code: [Select]
ITEM.Name = 'Respawn'
ITEM.Price = 20000
ITEM.Model = 'models/xqm/jetengine.mdl'
ITEM.SingleUse = true
ITEM.Except = true

function ITEM:OnEquip(ply, modifications)
if not GetConVarString("gamemode") == "terrortown" then print("Wrong gamemode. Please switch to TTT for this to work.")
end

if v:Alive() then return end

local corpse = corpse_find(v)
if corpse then corpse_remove(corpse) end

v:SpawnForRound( true )
v:SetCredits( ( (v:GetRole() == ROLE_INNOCENT) and 0 ) or GetConVarNumber("ttt_credits_starting") )

end
end

And here's the error I'm getting

Offline Avoid

  • Full Member
  • ***
  • Posts: 142
  • Karma: 42
Re: PointShop Respawn (for TTT)
« Reply #1 on: July 21, 2014, 03:45:48 pm »
Hello there,
please read the error message, it cleary states what is wrong!
You do have an extra, not needed end in your function..

Avoid

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: PointShop Respawn (for TTT)
« Reply #2 on: July 21, 2014, 04:15:09 pm »
Well, first of all, you're saying the player is v.
According to your code, you have not set v to be anything.
Out of the Garry's Mod business.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: PointShop Respawn (for TTT)
« Reply #3 on: July 21, 2014, 08:23:47 pm »
Hello there,
please read the error message, it cleary states what is wrong!
You do have an extra, not needed end in your function..

Avoid

I did, and I saw respawn.lua:20: '<eof>'
I had no idea what eof meant, so I decided to post here

Well, first of all, you're saying the player is v.
According to your code, you have not set v to be anything.
Alright, I changed every "v" to "ply", since I noticed that ply is used in other pointshop item files, and now when I buy Respawn from the PointShop, it does absolutely nothing. There's no errors or anything at all.

However, I'd like to use v instead of ply, so I guess I still have to define that

Again, I'm still pretty new to Lua, so how would I do that

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: PointShop Respawn (for TTT)
« Reply #4 on: July 21, 2014, 08:33:38 pm »
The error itself says '<eof>' expected near 'end' on line 20. '<eof>' stands for end of file, which means the error is saying that lua was expected the end of the file, but got 'end' instead. To fix, you'll need to check your functions and if statements, and make sure each one lines up with an "end" statement.

One thing to note, indentation helps immensely when coding, so by making sure everything is lined up properly, you can easily spot these errors. All of the code after your "function ITEM:OnEquip(ply, modifications)" is indented twice instead of once. If you fix that, your code will look like:

Code: Lua
  1. ITEM.Name = 'Respawn'
  2. ITEM.Price = 20000
  3. ITEM.Model = 'models/xqm/jetengine.mdl'
  4. ITEM.SingleUse = true
  5. ITEM.Except = true
  6.  
  7. function ITEM:OnEquip(ply, modifications)
  8.         if not GetConVarString("gamemode") == "terrortown" then print("Wrong gamemode. Please switch to TTT for this to work.")
  9.         end
  10.                
  11.         if v:Alive() then return end
  12.                
  13.         local corpse = corpse_find(v)
  14.         if corpse then corpse_remove(corpse) end
  15.  
  16.         v:SpawnForRound( true )
  17.         v:SetCredits( ( (v:GetRole() == ROLE_INNOCENT) and 0 ) or GetConVarNumber("ttt_credits_starting") )
  18.                
  19. end
  20. end

..and now you'll see that you have an extra end on line 20 that can be removed.


Secondly, what you're doing is modifying the "OnEquip" function, which (I assume) is called whenever your item is equipped. The function itself says:
Code: [Select]
function ITEM:OnEquip(ply, modifications)
When this function is called by TTT or whatever, it passes two parameters to your code: ply (the player object), and modifications (not sure, you'd have to look up documentation). Essentially, the first parameter will ALWAYS be the player object, and it will be named based on what you put there. If you want to be named "v", then you would change it to:
Code: [Select]
function ITEM:OnEquip(v, modifications)Keep in mind that changing to "v" or "ply" or "popsicle" really makes no difference- so long as the rest of your code refers to it the same way, you should be okay.

Hope that helps!
« Last Edit: July 21, 2014, 08:38:54 pm by Stickly Man! »
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: PointShop Respawn (for TTT)
« Reply #5 on: July 21, 2014, 08:38:39 pm »
Alright, so using what you said (and removing that extra end), it now looks like this
Code: Lua
  1. ITEM.Name = 'Respawn'
  2. ITEM.Price = 20000
  3. ITEM.Model = 'models/xqm/jetengine.mdl'
  4. ITEM.SingleUse = true
  5. ITEM.Except = true
  6.  
  7. function ITEM:OnEquip(ply, modifications)
  8.         if not GetConVarString("gamemode") == "terrortown" then print("Wrong gamemode. Please switch to TTT for this to work.")
  9.         end
  10.  
  11.         if v:Alive() then return end
  12.  
  13.         local corpse = corpse_find(v)
  14.         if corpse then corpse_remove(corpse) end
  15.  
  16.         v:SpawnForRound( true )
  17.         v:SetCredits( ( (v:GetRole() == ROLE_INNOCENT) and 0 ) or GetConVarNumber("ttt_credits_starting") )
  18.  
  19. end

However, it still does absolutely nothing when I buy the item. Still no errors either

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: PointShop Respawn (for TTT)
« Reply #6 on: July 21, 2014, 08:39:27 pm »
My apologies- I just edited my above post to add a second part- that should explain why nothing is happening.
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: PointShop Respawn (for TTT)
« Reply #7 on: July 21, 2014, 08:50:43 pm »
Alright, so I fixed the ply to v in the ITEM:OnEquip, so that it would match up with the rest of the code, and it still does nothing :(
Code: Lua
  1. ITEM.Name = 'Respawn'
  2. ITEM.Price = 20000
  3. ITEM.Model = 'models/xqm/jetengine.mdl'
  4. ITEM.SingleUse = true
  5. ITEM.Except = true
  6.  
  7. function ITEM:OnEquip(v, modifications)
  8.         if not GetConVarString("gamemode") == "terrortown" then print("Wrong gamemode. Please switch to TTT for this to work.")
  9.         end
  10.  
  11.         if v:Alive() then return end
  12.  
  13.         local corpse = corpse_find(v)
  14.         if corpse then corpse_remove(corpse) end
  15.  
  16.         v:SpawnForRound( true )
  17.         v:SetCredits( ( (v:GetRole() == ROLE_INNOCENT) and 0 ) or GetConVarNumber("ttt_credits_starting") )
  18.  
  19. end

That's the current state of the code


Sorry I have so many questions, I'm just really new to Lua and need help

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: PointShop Respawn (for TTT)
« Reply #8 on: July 21, 2014, 09:09:42 pm »
It's all good- hopefully you're learning!

Try adding a couple print statements- one on line 6 and one before line 8. You can print anything you want, just make it outlandish enough that you can see it in your console output.
The print on line 6 should go off as soon as your script loads. If you see that one, then you know your script is loading properly. The print between line 7-8 will display any time that function is called- if you don't see that one but you do see the print on line 6, then you know that whatever is supposed to be calling OnEquip isn't doing it's job (that's a more complicated issue to solve).
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: PointShop Respawn (for TTT)
« Reply #9 on: July 21, 2014, 09:14:45 pm »
You'll also need to change line 7 to
Code: Lua
  1. function ITEM:OnBuy( v )

Since this is a one time use thing.
Out of the Garry's Mod business.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: PointShop Respawn (for TTT)
« Reply #10 on: July 21, 2014, 09:30:10 pm »
As for Sticky Man!, both of the prints showed up, 6 when the server was starting up, and 8 when I ran the function

As for Neku, I changed OnEquip to OnBuy as you suggested, and now when I buy the item, it shows this error in console:


This is the code I have now (including the prints)
Code: Lua
  1. ITEM.Name = 'Respawn'
  2. ITEM.Price = 20000
  3. ITEM.Model = 'models/xqm/jetengine.mdl'
  4. ITEM.SingleUse = true
  5. ITEM.Except = true
  6. print("This is line 6 print")
  7. function ITEM:OnBuy(v, modifications)
  8. print("This is line 8 print (first line after function starts")
  9.         if not GetConVarString("gamemode") == "terrortown" then print("Wrong gamemode. Please switch to TTT for this to work.")
  10.         end
  11.  
  12.         if v:Alive() then return end
  13.  
  14.         local corpse = corpse_find(v)
  15.         if corpse then corpse_remove(corpse) end
  16.  
  17.         v:SpawnForRound( true )
  18.         v:SetCredits( ( (v:GetRole() == ROLE_INNOCENT) and 0 ) or GetConVarNumber("ttt_credits_starting") )
  19.  
  20. end

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: PointShop Respawn (for TTT)
« Reply #11 on: July 21, 2014, 11:49:56 pm »
As for Sticky Man!, both of the prints showed up, 6 when the server was starting up, and 8 when I ran the function

As for Neku, I changed OnEquip to OnBuy as you suggested, and now when I buy the item, it shows this error in console:


This is the code I have now (including the prints)
Code: Lua
  1. ITEM.Name = 'Respawn'
  2. ITEM.Price = 20000
  3. ITEM.Model = 'models/xqm/jetengine.mdl'
  4. ITEM.SingleUse = true
  5. ITEM.Except = true
  6. print("This is line 6 print")
  7. function ITEM:OnBuy(v, modifications)
  8. print("This is line 8 print (first line after function starts")
  9.         if not GetConVarString("gamemode") == "terrortown" then print("Wrong gamemode. Please switch to TTT for this to work.")
  10.         end
  11.  
  12.         if v:Alive() then return end
  13.  
  14.         local corpse = corpse_find(v)
  15.         if corpse then corpse_remove(corpse) end
  16.  
  17.         v:SpawnForRound( true )
  18.         v:SetCredits( ( (v:GetRole() == ROLE_INNOCENT) and 0 ) or GetConVarNumber("ttt_credits_starting") )
  19.  
  20. end

You haven't set corpse_find/corpse_remove.
Out of the Garry's Mod business.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: PointShop Respawn (for TTT)
« Reply #12 on: July 22, 2014, 01:08:05 am »
And the error says that. "corpse_find" = nil value.
In programming, Nil equals nothing. Error indicates it expected something
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: PointShop Respawn (for TTT)
« Reply #13 on: July 22, 2014, 12:48:05 pm »
Alright, after looking through the file with the group of TTT commands, I found corpse_find, corpse_remove, and corpse_identify all defined in there.

So I copied those three functions and moved 'em on over to my respawn item, tested it, and it works perfectly fine. Thanks for all of the help, Avoid, Neku, and StickyMan!.


Just for reference, here's the final code I used:
Code: Lua
  1. ITEM.Name = 'Respawn'
  2. ITEM.Price = 20000
  3. ITEM.Model = 'models/xqm/jetengine.mdl'
  4. ITEM.SingleUse = true
  5. ITEM.Except = true
  6.  
  7. function ITEM:OnBuy(v, modifications)
  8.         if not GetConVarString("gamemode") == "terrortown" then print("Wrong gamemode. Please switch to TTT for this to work.")
  9.         end
  10.  
  11.         if v:Alive() then return end
  12.  
  13.         local corpse = corpse_find(v)
  14.         if corpse then corpse_remove(corpse) end
  15.  
  16.         v:SpawnForRound( true )
  17.         v:SetCredits( ( (v:GetRole() == ROLE_INNOCENT) and 0 ) or GetConVarNumber("ttt_credits_starting") )
  18.  
  19. end
  20.  
  21. function corpse_find(v)
  22.         for _, ent in pairs( ents.FindByClass( "prop_ragdoll" )) do
  23.                 if ent.uqid == v:UniqueID() and IsValid(ent) then
  24.                         return ent or false
  25.                 end
  26.         end
  27. end
  28.  
  29. function corpse_remove(corpse)
  30.         CORPSE.SetFound(corpse, false)
  31.         if string.find(corpse:GetModel(), "zm_", 6, true) then
  32.                 corpse:Remove()
  33.         elseif corpse.player_ragdoll then
  34.                 corpse:Remove()
  35.         end
  36. end
  37.  
  38. function corpse_identify(corpse)
  39.         if corpse then
  40.                 local ply = player.GetByUniqueID(corpse.uqid)
  41.                 ply:SetNWBool("body_found", true)
  42.                 CORPSE.SetFound(corpse, true)
  43.         end
  44. end

  • Print