Need Help with sending an entity from the client to the server.
7 replies, posted
Hello. I am writing a SWEP, and I have this following code:
if SERVER then
util.AddNetworkString( "263TargetStr" )
end
if CLIENT then
hook.Add("PlayerBindPress", "SCP263SetTarget", function(ply, bind)
wep = ply:GetWeapon("weapon_scp263")
if !wep:IsValid() then return end
if bind == "+use" then
ent = ply:GetEyeTrace().Entity
if (ent:IsPlayer() or ent:IsNPC()) then
wep.Target = ent
end
end
end)
hook.Add("PreDrawHalos","SCP263DrawTargetHalo",function()
wep = LocalPlayer():GetActiveWeapon()
if !wep:IsValid() or wep:GetClass() != "weapon_scp263" then return end
if !IsValid(wep.Target) or !wep.Target:Alive() then wep.Target = NULL return end
halo.Add({wep.Target},Color(255,0,0),2,2,1,true,true)
target = wep.Target
net.Start( "263TargetStr" )
net.WriteEntity( target )
net.SendToServer()
end)
end
function SWEP:PrimaryAttack()//Ignite Target
if SERVER then
net.Receive( "263TargetStr", function( target )
net.ReadEntity()
if IsValid(target) and target:Alive() then
target:Ignite( 100000 )
ServerLog( "[SCP-263] " .. target .. " was ignited by " .. self.Owner )
else
return
end
end)
end
end
function SWEP:SecondaryAttack() //RNG
if SERVER then
chance = math.random( 1, 1000000 )
net.Receive( "263TargetStr", function ( target )
if chance < 1000 then//Lv5 Prize - 1 in 1000
target:Give()
target:GiveAmmo()
target:SetHealth( target:GetMaxHealth() * 20 )
target:SetMaxHealth( target:GetMaxHealth() * 20 )
target:SetArmor( target:Armor() + 500 )
target:SCP_Invtargetory_GiveLoadout( "card_lvl_5" )
ServerLog( "[SCP 263] Level 5 Prize generated by " .. self.Owner .. " for " .. target )
elseif chance < 10000 then//Lv4 Prize - 1 in 100
target:Give()
target:GiveAmmo()
target:SetHealth( target:GetMaxHealth() * 10 )
target:SetMaxHealth( target:GetMaxHealth() * 10 )
target:SetArmor( target:Armor() + 250 )
target:SCP_Invtargetory_GiveLoadout( "card_lvl_5" )
ServerLog( "[SCP 263] Level 4 Prize generated by " .. self.Owner .. " for " .. target )
elseif chance < 100000 then//Lv3 Prize - 1 in 10
target:Give()
target:GiveAmmo()
target:SetHealth( target:GetMaxHealth() * 5 )
target:SetMaxHealth( target:GetMaxHealth() * 5 )
target:SetArmor( target:Armor() + 150 )
target:SCP_Invtargetory_GiveLoadout( "card_lvl_5" )
ServerLog( "[SCP 263] Level 3 Prize generated by " .. self.Owner .. " for " .. target )
elseif chance < 500000 then//Lv2 Prize - 1 in 2
target:Give()
target:GiveAmmo()
target:SetHealth( target:GetMaxHealth() * 2 )
target:SetMaxHealth( target:GetMaxHealth() * 2 )
target:SetArmor( target:Armor() + 50 )
target:SCP_Invtargetory_GiveLoadout( "card_lvl_5" )
ServerLog( "[SCP 263] Level 2 Prize generated by " .. self.Owner .. " for " .. target )
elseif chance <= 1000000 then //Lv1 Prize - 1 in 2
target:Give()
target:GiveAmmo()
target:SCP_Invtargetory_GiveLoadout( "card_lvl_5" )
ServerLog( "[SCP 263] Level 1 Prize generated by " .. self.Owner .. " for " .. target )
end
end)
end
end
function SWEP:Reload()//Clear Target
wep.Target = NULL
end
The issue: Whenever I have a target selected and use Primary or Secondary attack, it in console says "attempt to index global 'ent' (a nil value) on line 91.
Please help!
I think the issue is that it won't recieve the "target" entity, or I just wrote the net stuff wrong, but I don't have any experience with net.
You cannot use net.Receive() inside of any other functions (as stated on the wiki). Therefore, you cannot place your net.Receive() call inside of your SWEP's functions.
Should I just put it somewhere above the SWEP functions then?
net.Receive() IS a function
okay, but how do I use it to let me use the variable target in SWEP:Primary and Secondary?
you gotta send the message from somewhere, attack function fo example
net.Start( "263TargetStr" )
net.WriteEntity( target )
net.SendToServer()
and then put this outside of any hook (function) also read the comments i put here
if SERVER then
net.Receive( "263TargetStr", function( len, ply ) --there are two arguments and they are not the stuff youre sending. its length of the message and player who sent it, you can find more info on this on the wiki
local target = net.ReadEntity() --thats how you read stuff from net messages
--and then do you stuff
if IsValid(target) and target:Alive() then
target:Ignite( 100000 )
ServerLog( "[SCP-263] " .. target .. " was ignited by " .. self.Owner )
else
return
end
end)
end
read this if you dont understand how to use net.messages
Net Library Usage
but the problem is the function that Ignites target should be the primary attack, or can that not be done?
Fixed.
Sorry, you need to Log In to post a reply to this thread.