• Spray blocker help
    7 replies, posted
Okay so i'm making this script to block people from spraying on certain people's spray (aka admin). I am an experienced programmer with Java, VB, and some C++. I have never done lua before. I am embedding my code into a script called trace_dem_sprays. My main question is how do i get this to work? if SERVER then AddCSLuaFile("trace_dem_sprays.lua") function ResetSpray( ply ) ply:SetNWVector( "SprayPos", Vector( 0, 0, 0 ) ) end hook.Add( "PlayerInitialSpawn", "ResetSpray", ResetSpray ) function OnSpray( ply ) local pos = ply:GetEyeTrace().HitPos ply:SetNWVector( "SprayPos", pos ) end hook.Add( "PlayerSpray", "HandleSpray", OnSpray ) function ChatCommandSpray(ply, text) if ( text == "!spray" or text == "!spraytrace" or text == "!tracespray") then ply:ConCommand("spraytrace") return "" end end hook.Add("PlayerSay", "ChatCommandSpray", ChatCommandSpray) end if CLIENT then adminspray = 0 function ShowSprayOwner() local Trace = LocalPlayer():GetEyeTrace() local LookAt = Trace.HitPos local demcounter = 0 for _, pl in pairs(player.GetAll()) do if demcounter > 0 then break end local SPos = pl:GetNWVector( "SprayPos" ) if SPos != Vector(0, 0, 0) and LookAt:Distance( SPos ) < 32 and Trace.HitWorld and LocalPlayer():GetPos():Distance( SPos ) < 400 then local Text = pl:Nick() .. "'s Spray" Text2 = pl:SteamID() surface.CreateFont( "treb_smal", { font = "treb_small", size = 14, weight = 40000, antialias = true, shadow = true}) local w, h = surface.GetTextSize( Text ) local w2, h2 = surface.GetTextSize( Text2 ) w = w + 5 h = h + 5 w2 = w2 + 5 h2 = h2 + 5 demcounter = demcounter + 1 draw.WordBox( 6, (ScrW()/2)-(w/2), (ScrH()/2)-(h/2), Text, "treb_smal", Color( 0, 0, 0, 200 ), Color(255,255,255,255) ) draw.WordBox( 6, (ScrW()/2)-(w2/2), (ScrH()/2)-(h2/2) + 28, Text2, "treb_smal", Color( 0, 0, 0, 200 ), Color(255,255,255,255) ) end end end hook.Add( "HUDPaint", "ShowSprayOwner", ShowSprayOwner ) function PrintSprayOwner() local Trace = LocalPlayer():GetEyeTrace() local LookAt = Trace.HitPos for _, pl in pairs(player.GetAll()) do local SPos = pl:GetNWVector( "SprayPos" ) if SPos != Vector(0, 0, 0) and LookAt:Distance( SPos ) < 32 and Trace.HitWorld and LocalPlayer():GetPos():Distance( SPos ) < 600 then local Text = pl:Nick() .. "'s" local Text2 = " (" .. pl:SteamID() .. ") " chat.AddText(Color(255,255,255,255),Text,Color(255,255,255,255),Text2,Color(255,255,255,255),"Spray.") end end end concommand.Add("spraytrace",PrintSprayOwner) --below is my code. Everything above works hook.Add("PlayerSpray", function(ply) if (Text2 == "STEAM_0:0:9310913" or "STEAM_0:1:45363532")then adminspray = 1 else adminspray = 0 end -- if the player is admin then he can spray no matter what if ply:IsAdmin() then return false end --Stops players sprays if (adminspray == 1)then ply:ChatPrint("You aren't allowed to use sprays!") return true end return true end) end
use [.lua.][./lua] tags without the periods of course.
Why not just do [lua] hook.Add("PlayerSpray", function(ply) if IsValid(ply) and ply:IsAdmin() then print("you sprayed your spray!") return false elseif IsValid(ply) and not ply:IsAdmin() then print("you cannot spray your spray!") return true end end) [/lua] the code was writen without me looking at the wiki to check for function errors, It may need some minor adjustments. also [code] if (Text2 == "STEAM_0:0:9310913" or "STEAM_0:1:45363532")then [/code] will never be true because [code] local Text2 = " (" .. pl:SteamID() .. ") " [/code] it has ()'s in it You could however do [code] if (ply:SteamID() == "STEAM_0:0:9310913" or "STEAM_0:1:45363532")then --bla end [/code]
Im trying to make it so other can't spray on admins sprays, not stop them from spraying. hence my attempt at getting text2 because the trace dem_sprays_scrip displays their steam ids. so if text2 = "id" --meaning their looking at a spray. abletospray = false something like that.
So you don't want people to spray over each others sprays?
[QUOTE=Pandaman09;39493728]Why not just do [lua] hook.Add("PlayerSpray", function(ply) if IsValid(ply) and ply:IsAdmin() then print("you sprayed your spray!") return false elseif IsValid(ply) and not ply:IsAdmin() then print("you cannot spray your spray!") return true end end) [/lua] the code was writen without me looking at the wiki to check for function errors, It may need some minor adjustments. also [code] if (Text2 == "STEAM_0:0:9310913" or "STEAM_0:1:45363532")then [/code] will never be true because [code] local Text2 = " (" .. pl:SteamID() .. ") " [/code] it has ()'s in it You could however do [code] if (ply:SteamID() == "STEAM_0:0:9310913" or "STEAM_0:1:45363532")then --bla end [/code][/QUOTE] That will always be true because strings are considered as true in conditions. Which means ultimately your code is wrong. [lua] if (ply:SteamID() == "STEAM_0:0:9310913" or ply:SteamID() == "STEAM_0:1:45363532")then --bla end [/lua]
@pandaman09 your looking at the wrong text2. and no i don't want people to spray over admins sprays ("or steam ids i define in the code")
Bump
Sorry, you need to Log In to post a reply to this thread.