I’m trying to make it so my script runs for players with a certain Attribute and if the value of that Attribute is found inside of the table the script would run, I tried to do that myself by doing;
local Table = {"Value"}
if table.find(Table, Player.Character:GetAttribute("Value")) then
-- code here
end
But the script doesn’t work despite me doing that, how would I go about doing what I want to do though without any errors?
Whats the context? server or client?
You did set the attribute under the same context?
What you mean you want the script runs for that player that has the Attribute? each player has that script? (client/local) and that will check if the player has the attribute and decide if continue?..
Basically I’m trying to make a regeneration system for the attributes; there’s a max capacity and the current value it’s at.
When the current attribute value falls below the max capacity it’s supposed to add +1 value to the current attribute value until it reaches the max capacity again but when I’m trying to do that it won’t work unless it’s a script code but here’s the whole thing;
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- > Delays:
local Magic_Regeneration_Delay = 0.3
-- > Tables:
local Witch_Table = {"Witch"}
-- > Code:
coroutine.wrap(function()
while task.wait(Magic_Regeneration_Delay) do
for _, Player in ipairs(game.Players:GetPlayers()) do
task.spawn(function()
if Player.Character then
if Player.Character:GetAttribute("Using_Character") == true then
if table.find(Witch_Table, Player.Character:GetAttribute("Specie")) then
if Player.Character:GetAttribute("Magic") ~= nil then
if Player.Character:GetAttribute("Magic") < Player.Character:GetAttribute("Magic_Capacity") and not (Player.Character:GetAttribute("Powerless") == true or Player.Character:FindFirstChild("Dead")) then
Player.Character:SetAttribute("Magic", Player.Character:GetAttribute("Magic") + 1)
end
end
end
end
end
end)
end
end
end)()
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I realise my original answer was wrong so here, basically :GetAttribute() returns the value with it too. So I guess you can use that in a way. You could use :GetAttributes() which returns all the attributes a player has though.
Yeah, I think it’s fine. It should work as expected but you could optimize it. doing while loops and task.spawn inside a coroutine.wrap may be very bad in terms of performance. But I was wrong about attributes, I don’t really use them but I have used datastores before so I assumed the returned value from getattribute was a tuple. But it wasn’t, you can only save one value per attribute I believe (correct me if im wrong), if that’s not the case then the use of your witch_Table is correct. And the only thing you might need to fix is getting rid of unneccessary while loops and task library functions inside of your coroutine.wrap which would cause performance issues.
coroutine.wrap(function()
while task.wait(Magic_Regeneration_Delay) do
for _, players in ipairs(game.playerss:Getplayerss()) do
if players.Character then
if players.Character:GetAttribute("Using_Character") == true then
if table.find(Witch_Table, players.Character:GetAttribute("Specie")) then
if players.Character:GetAttribute("Magic") ~= nil then
if players.Character:GetAttribute("Magic") < players.Character:GetAttribute("Magic_Capacity") and not (players.Character:GetAttribute("Powerless") == true or players.Character:FindFirstChild("Dead") or players.Character.Reset.Value == true or players.Character.Humanoid.Respawn.Value == true) then
players.Character:SetAttribute("Magic", players.Character:GetAttribute("Magic") + 1)
end
end
end
end
end
end
end
end)()
Also the meaning of the table is a list of names that the attribute can have and if the attribute value is found in the table then it will run the script but that doesn’t seem to be working.
I guess that’s because it’s an array, an array is not a value you know? Maybe try something like this.
local attributes = players.Character:GetAttributes(AttributeName) -- Returns an array of attributes
if table.find(attributes, "Value") then
print("Value found!")
end
If you have multiple values in an attribute (which to my knowledge isn’t possible) then you can use either orand statements in the if statement. So like “Value1” or “Value2”
Heya! Seems you’re trying to compare two tables (attributes are just a table if you think about it), so here’s some code to accomplish this:
local function AttributeInTable(myInstance,myTable)
for _,value in myInstance:GetAttributes() do
if table.find(myTable,value) then
return true
end
end
return false
end
Well, I tested your script, I don’t exactly find the issue, it works to me. I would suggest some improvements later. (like Set the attributes into the Player instance not the character model cause it could get eventually destroyed, and change the approach so the loop is not constantly trying to increase magic on players that dont need it)
I tested your script with this, try this, read the stuff I changed to your script and read the output, it works as expected:
-- > Delays:
local Magic_Regeneration_Delay = 0.3
-- > Tables:
local Witch_Table = {"Witch", "Elf"}
game.Players.PlayerAdded:Connect(function(ply)
local CHAR = ply.Character or ply.CharacterAdded:Wait()
CHAR:SetAttribute("Using_Character", true)
CHAR:SetAttribute("Specie", "Witch") -- CHAR:SetAttribute("Specie", "Elf")
CHAR:SetAttribute("Magic", 5)
CHAR:SetAttribute("Magic_Capacity", 50)
CHAR:SetAttribute("Powerless", false) -- CHAR:SetAttribute("Powerless", true)
CHAR:SetAttribute("Dead", false) -- CHAR:SetAttribute("Dead", true)
end)
-- > Code:
coroutine.wrap(function()
while task.wait(Magic_Regeneration_Delay) do
for _, Player in ipairs(game.Players:GetPlayers()) do
task.spawn(function()
local CHAR = Player.Character
if CHAR then
warn(CHAR)
local MagicAtt = CHAR:GetAttribute("Magic")
print("Magic value:", MagicAtt)
print("Specie:",Player.Character:GetAttribute("Specie"))
print("Magic_Capacity:",Player.Character:GetAttribute("Magic_Capacity"))
print("Powerless:",Player.Character:GetAttribute("Powerless"))
print("Dead:",Player.Character:GetAttribute("Dead"))
if CHAR:GetAttribute("Using_Character") then
if table.find(Witch_Table, Player.Character:GetAttribute("Specie")) then
warn("Found specie", Player.Character:GetAttribute("Specie"),"at key:", table.find(Witch_Table, Player.Character:GetAttribute("Specie")))
if MagicAtt ~= nil then
if MagicAtt < CHAR:GetAttribute("Magic_Capacity") then
if not CHAR:GetAttribute("Powerless") then
if not CHAR:GetAttribute("Dead") then
CHAR:SetAttribute("Magic", MagicAtt + 1)
warn("Magic value increased to:", CHAR:GetAttribute("Magic"))
else
warn("Player is dead, unable to increase Magic value")
end
else
warn("Player is powerless, unable to increase Magic value")
end
else
warn("Magic is full")
end
end
end
end
end
end)
end
end
end)()