table.HasValue and PlayerSpawnProp / spawned model path?
8 replies, posted
Hey there,
I want to check if the prop a player is spawning is inside of my table. But I'm messing up..
local props = {
"models/props_c17/oildrum001_explosive.mdl",
}
if table.HasValue( props, ??? ) then
print("Explosive spawned")
end
I found SANDBOX:PlayerSpawnProp but I'm not sure what I'm doing wrong (cant work with this to call for a table) or if there's a better way.
Hope someone could be that Kind to help me out.
Cheers
local explosives = {
["models/props_c17/oildrum001_explosive.mdl"] = true,
}
hook.Add("PlayerSpawnedProp", "IsExplosive", function(ply, strModel, ent)
if explosives[strModel] then
print("Explosive spawned")
end
end)
Thanks for your help and answer but the explosive is just an example. I want to add other tables / props too with different checks inside one if statement. That's why I'm working with table.HasValue().
So is there another solution without this hook?
I'm not that experienced - something like table.HasValue( props, getSpawnedEntityModel() ).
It's frustrating that I'm not getting behind that without help for now.
table.HasValue is not efficient with large tables, just rename that table (if you don't like explosives as its name, lol) and add your models
local props = {
["your_prop_model"] = true,
["your_prop_model_2"] = true,
}
I know that I can just rename it but here's an example of what I'll check over there:
local xProps = {
"models/props_c17/oildrum001_explosive.mdl",
"xymodel",
}
local xRanks = {
"SuperAdmin",
"Admin",
"AnyRank",
}
if table.HasValue( xRanks, ply:GetNWString("usergroup") ) and table.HasValue( xProps, spawnedprop? ) then
print("worked")
end
So if I'm right it should work with your solution like this:
local xProps = {
["models/props_c17/oildrum001_explosive.mdl"] = true,
["xymodel"] = true,
}
local xRanks = {
"SuperAdmin",
"Admin",
"AnyRank",
}
local function propRankStuffThing(ply, strModel, ent)
if xProps[strModel] then
return true
end
return false
end
if table.HasValue( xRanks, ply:GetNWString("usergroup") ) and table.HasValue( xProps, propRankStuffThing ) then
print("worked")
end
hook.Add("PlayerSpawnedProp", "RankPropStuff", propRankStuffThing)
is that correct?
Just working with different tables (not large ones) but I'll add different conditions to different collections. Rank is also an example. Just need to understand the way and I'm not sure but this could work what I wrote now or did I misunderstand(?) that?
local xProps = {
["models/props_c17/oildrum001_explosive.mdl"] = true,
["xymodel"] = true,
}
local xRanks = {
"SuperAdmin",
"Admin",
"AnyRank",
}
local function propRankStuffThing(ply, strModel, ent)
if table.HasValue( xRanks, ply:GetNWString("usergroup") ) and xProps[strModel] then
print("worked")
end
if xProps[strModel] then
return true
end
return false
end
hook.Add("PlayerSpawnProp", "RankPropStuff", propRankStuffThing)
Sorry, I used the wrong hook, it's PlayerSpawnProp (PlayerSpawnedProp is called after the prop was spawned)
local bannedProps = {
["models/props_c17/oildrum001_explosive.mdl"] = true,
}
local allowedRanks = {
["superadmin"] = true,
["admin"] = true,
}
hook.Add("PlayerSpawnProp", "RankProps", function(ply, model)
if bannedProps[model] and not allowedRanks[ply:GetUserGroup()] then
return false
end
end)
Oh didn't looked on that too but saw that on gmod wiki before. Just didnt knew how to use this in this case.
Thank you for your help and just to understand for the future - xProps[strModel] is called when a player spawns the prop (cause the hook) and it's in the xProps list - also marked with "= true". Right?
Yes. Read about programming basics (or watch CS50, it really helps)
Can you not disagree with my post when I just fixing his errors?
Sorry, you need to Log In to post a reply to this thread.