Hi, I've made a Script for my SCP Roleplay server which make a HandIcon when you watch a button or a door.
Howerer it's quite glitched and I don't know why.
We are 40 players on the server and some people see it, some peolple not, sometimes it works and then disapear...
Here is the code, it's in a .lua in lua/autorun/
[CODE]if CLIENT then
local function drawhandicon()
local tr = util.GetPlayerTrace( LocalPlayer(), LocalPlayer():GetAimVector() )
local trace = util.TraceLine( tr )
if (!trace.Hit) then return end
if (!trace.HitNonWorld) then return end
if LocalPlayer():GetNWBool("lookatabutton") then
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( Material("scprp/handsymbol.png"))
surface.DrawTexturedRect( ScrW()/2-32, ScrH()/2-32, 64, 64 )
end
end
hook.Add( "HUDPaint", "drawhandicon",drawhandicon )
end
if SERVER then
AddCSLuaFile("handicon.lua")
resource.AddFile("materials/scprp/handsymbol.png")
function gethandicon()
for k, v in pairs(player.GetAll()) do
local tr = util.GetPlayerTrace( v, v:GetAimVector() )
local trace = util.TraceLine( tr )
if (!trace.Hit) then v:SetNWBool("lookatabutton",false) return end
if (!trace.HitNonWorld) then v:SetNWBool("lookatabutton",false) return end
if (trace.HitPos:Distance(v:GetPos()) > 200) then v:SetNWBool("lookatabutton",false) return end
if trace.Entity:GetClass() == "func_button" or trace.Entity:GetClass() == "func_door" or trace.Entity:GetClass() == "func_door_rotating" or trace.Entity:GetClass() == "prop_door_rotating" then
v:SetNWBool("lookatabutton",true)
else
v:SetNWBool("lookatabutton",false)
end
end
end
hook.Add("Think", "gethandicon", gethandicon)
end
[/CODE]
Do you need to do the check serverside? At first glance it seems like it could be done in the client, leaving only the resource.AddFile and AddCSLuaFile lines (and removing the need for the networked var).
I tried to explain this to you as best as a could, so please read it before you paste it into your script. It'll help greater your understanding of lua.
[CODE]hitFuncs = { -- Table of items that if it hits it'll create a hand.
["func_button"] = true,
["func_door"] = true,
["func_door_rotating"] = true,
["prop_door_rotating"] = true,
}
hook.Add( "HUDPaint", "handIconOnDoor", function() -- HUDPaint hook
if IsValid( LocalPlayer():GetEyeTrace().Entity ) and hitFuncs[LocalPlayer():GetEyeTrace().Entity:GetClass()] then -- Validating the eyetrace and checking if it's class is in the table
surface.SetDrawColor( 255, 255, 255, 255 ) -- Next three lines draw the hand (your code)
surface.SetMaterial( Material("scprp/handsymbol.png"))
surface.DrawTexturedRect( ScrW()/2-32, ScrH()/2-32, 64, 64 )
end -- Ending the if statement
end ) -- Ending the hook[/CODE]
Thanks for your help !
[editline]9th January 2016[/editline]
Oh, I've just seen it doesn't work with the button :/
It works with all doors but not buttons.
[QUOTE=annilator3000;49494108]Thanks for your help !
[editline]9th January 2016[/editline]
Oh, I've just seen it doesn't work with the button :/
It works with all doors but not buttons.[/QUOTE]
What kind of button is it? It might not be a "func_button" but classified as something else if it is a part of the map. Try printing out the name of the entity the player is looking at, and making sure that it is in that table.
[QUOTE=Promptitude;49492907]I tried to explain this to you as best as a could, so please read it before you paste it into your script. It'll help greater your understanding of lua.
[CODE]hitFuncs = { -- Table of items that if it hits it'll create a hand.
["func_button"] = true,
["func_door"] = true,
["func_door_rotating"] = true,
["prop_door_rotating"] = true,
}
hook.Add( "HUDPaint", "handIconOnDoor", function() -- HUDPaint hook
if IsValid( LocalPlayer():GetEyeTrace().Entity ) and hitFuncs[LocalPlayer():GetEyeTrace().Entity:GetClass()] then -- Validating the eyetrace and checking if it's class is in the table
surface.SetDrawColor( 255, 255, 255, 255 ) -- Next three lines draw the hand (your code)
surface.SetMaterial( Material("scprp/handsymbol.png"))
surface.DrawTexturedRect( ScrW()/2-32, ScrH()/2-32, 64, 64 )
end -- Ending the if statement
end ) -- Ending the hook[/CODE][/QUOTE]
You should cache that Material.
It's my map and the button entities are func_button .
Why not use the hook [B]HUDDrawTargetID[/B]?
This hook already runs with HUDPaint and also automatically does a trace of the crosshair
Sorry, you need to Log In to post a reply to this thread.