I'm working on an addon so you can eat food to regain health and etc, but I've ran into a problem. It works up until the point where the entity that has been eaten needs to be removed which then it removes the player (not sure if it removes the food too). I'm stumped on why it's doing this.
client:
[code]
function FoodInteraction()
if !LocalPlayer():Alive() then
return
local ent = LocalPlayer():GetEyeTrace().Entity
local EntityModelName = ent:GetModel()
EntityModelName = EntityModelName:split("/")
EntityModelName = EntityModelName[tablelength(EntityModelName)]
-- too far away, do nothing
if LocalPlayer():EyePos():Distance(ent:GetPos()) > 75 then
return
if !IsValidFood(EntityModelName) then
return
if input.IsKeyDown(KEY_E) then
-- can't eat for another 2 seconds
if LastAteTime + 2 > os.time() then
return
LastAteTime = os.time()
-- send message to server
net.Start("EatFood")
net.WriteEntity(ent)
net.WriteEntity(LocalPlayer())
net.SendToServer()
end
end
[/code]
server:
[code]
util.AddNetworkString("EatFood")
net.Receive( "EatFood", function( len, ent, player )
ent:Remove()
player:SetHealth(math.min(100, player:GetHealth() + 5))
end)
[/code]
It removes the player because the second argument in your function in net.Receive is the player sending the net message.
[url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url]
Sorry, you need to Log In to post a reply to this thread.