input.IsKeyDown only fire once? and GetName Nil on loading?
5 replies, posted
1st problem...
I have been working on a door ownership system for my game mode, however I need it so that input.IsKeyDown only runs once per key press.
When it runs normally (code at the bottom) it starts spamming and makes it hard to get the script to do what you want to do.
What I need is something like input.WasKeyReleased in the code, however when I try to use input.WasKeyReleased nothing happens, no errors and no output.
the code is in init.lua (for ent:Fire ) and in doortext.lua (everything else)
Problem 2.
This is probably something really simple I am missing in my code.
When I first load up the server (using a listen server for now) it throws the error [CODE][ERROR] gamemodes/hrp/gamemode/visuals/doortext.lua:2: attempt to call method 'GetName' (a nil value)
[/CODE]
From what I can tell, it means that it cannot get my name on start up even though this is a client script on line 2
However if I edit the code in any way (don't even need to add anything, just adding a space will do) than the code loads up fine and works...
CODE:
init.lua
[CODE]AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "visuals/HUD.lua" )
AddCSLuaFile( "visuals/VGUI.lua" )
AddCSLuaFile( "visuals/doortext.lua" )
AddCSLuaFile( "autorun/cl_roll.lua" )
AddCSLuaFile( "autorun/cl_falleffect.lua" )
AddCSLuaFile( "autorun/HAA.lua" )
AddCSLuaFile( "visuals/doortext.lua" )
include( "shared.lua" )
include( "jobs/weapons.lua" )
DeriveGamemode("sandbox")
function jointeam( jointeam, ply )
ply:SetTeam( jointeam )
end
function defteam( ply )
ply:SetTeam( 1 )
end
concommand.Add( "hrp_reset_team", function( ply )
ply:Kill()
ply:SetTeam( 1 )
print( ply:Team() )
end)
concommand.Add( "hrp_unlockdoor", function( ply, cmd, args, str )
local inplyname = args[1]
local ent = ents.GetByIndex( math.Round( args[2] ) )
ent:Fire( "unlock" )
end )
concommand.Add( "hrp_lockdoor", function( ply, cmd, args, str )
local inplyname = args[1]
local ent = ents.GetByIndex( math.Round( args[2] ) )
ent:Fire( "lock" )
end )[/CODE]
shared.lua
[CODE]function GM:Initialize()
end
include( "visuals/HUD.lua" )
include( "visuals/VGUI.lua" )
function Spawn ( ply )
ply:SetTeam( 1 )
ply:SetCanZoom( false )
end
hook.Add( "PlayerSpawn", "playerSpawn", Spawn )
for k, v in pairs(player.GetHumans()) do
v:SetCanZoom( false )
end
[/CODE]
cl_init.lua
[CODE]DeriveGamemode("sandbox")
include( "shared.lua" )
include( "autorun/cl_roll.lua" )
include( "autorun/cl_falleffect.lua" )
include( "autorun/HAA.lua" )
include( "visuals/doortext.lua" )
ply = LocalPlayer()
function GM:SpawnMenuEnabled()
return true;
end[/CODE]
doortext.lua
[CODE]ply = LocalPlayer()
plyname = ply:GetName()
function decdoortext()
ent = LocalPlayer():GetEyeTrace().Entity
plrvec = LocalPlayer():GetPos()
entvec = ent:GetPos()
if ent:GetClass() == "prop_door_rotating" and entvec:Distance( plrvec ) < 150 then
lookingatdoor = true
if ent:GetOwner().Nick != nil then
entowner = ent:GetOwner()
entownername = entowner:GetName()
DoorText = "Door Owned By "..entowner:GetName()
if entownername == plyname then
DoorText1 = "Press X to Lock/Unlock Door"
DoorText2 = "Double tap X to Sell Door"
if input.IsKeyDown( KEY_X ) then
if ent.locked == true then
RunConsoleCommand( "hrp_unlockdoor", ply:GetName(), ent:EntIndex() )
ent.locked = false
elseif ent.locked == false then
RunConsoleCommand( "hrp_lockdoor", ply:GetName(), ent:EntIndex() )
ent.locked = true
end
end
else
DoorText1 = ""
DoorText2 = ""
end
else
DoorText = "Door Not Owned, Press X to buy"
if input.IsKeyDown( KEY_X ) then
ent:SetOwner( LocalPlayer() )
print( plyname.." Is Owning A Door" )
ent.locked = false
RunConsoleCommand( "hrp_unlockdoor", ply:GetName(), ent:EntIndex() )
end
end
else
DoorText = ""
DoorText1 = ""
DoorText2 = ""
end
end
hook.Add( "Think", "DoorThink", decdoortext )
function PaintWordBox()
draw.SimpleText(DoorText or "", "Trebuchet24", ScrW() / 2 - 70, ScrH() - 83, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText(DoorText1 or "", "Trebuchet24", ScrW() / 2 - 70, ScrH() - 63, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText(DoorText2 or "", "Trebuchet24", ScrW() / 2 - 70, ScrH() - 43, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
hook.Add( "HUDPaint", "WordBoxPaint", PaintWordBox )[/CODE]
Keep in mind that these are the whole files, so any errors in a line will be the same here.
Thanks in advance
In your doortext.lua, change line 2 to
[lua]plyname = ply:Name()[/lua]
Also, why are you setting the owner clientside? It's a server side function
[url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index38bf.html[/url]
And for your command running multiple times, you could use KeyPressReleased instead.
[QUOTE=Tomvdr;42344685]In your doortext.lua, change line 2 to
[lua]plyname = ply:Name()[/lua]
Also, why are you setting the owner clientside? It's a server side function
[url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index38bf.html[/url]
And for your command running multiple times, you could use KeyPressReleased instead.[/QUOTE]
Changing GetName() to Name() Changed nothing in the script, however as I said in the OP once I edit the script it loads fine.
I dont know where I can find the syntax for the KeyPressReleased, so I assumed that you meant KeyRelease, and that did not work either :/
However I was able to set the ownership to serverside, thanks for that :)
EDIT:
Don't use that wiki, it has not been updated for Gmod 13 (I learnt that the hard way)
Bump
Can someone help me at all?
also, nothing changes when I use a dedicated server :/
1) Use input.KeyPressed or input.KeyReleased
2) You are not defining ply on line 2, obviously it is an error. LocalPlayer is nil at the very start of the server. Solution: move your plyname stuff right before you need it
[QUOTE=Robotboy655;42391805]1) Use input.KeyPressed or input.KeyReleased
2) You are not defining ply on line 2, obviously it is an error. LocalPlayer is nil at the very start of the server. Solution: move your plyname stuff right before you need it[/QUOTE]
Thank you so much :) working like a charm.
Sorry, you need to Log In to post a reply to this thread.