What do you want to achieve? gates on workspace that are named Gate1 Gate2 Gate3 and Gate4 and the for loop destroys every part but if the player doesnt have enough value the gate wont get destroyed
What is the issue? no idea on how to do it
What solutions have you tried so far? tried making code like this
for i, v in pairs(world:GetDescendants()) do
if v.Name == "Gate"..tostring(gatesValue.Value) then
v:Destroy()
end
end
What is “world”, is that a variable? What do you want to loop through?
Also, try printing “Gate”…tostring(gatesValue.Value) and see if that’s the string you want.
the world variable is a workspace. i wanna loop through every gate on workspace and (locally) delete them depending on the players value
i already tried printing and it worked
Try adding prints. Assuming world is game:GetService(“Workspace”) or game.Workspace, then the loop should work. Add a print to check though. Is gatesValue an intvalue?
yes gatesvalue is an intValue
and the script works but it doesnt remove the other gates named Gate1 and Gate2 since my player value is 3
and it prints the right name
for i, v in pairs(world:GetDescendants()) do
local val = gatesValue.Value
if string.sub(v.Name, 1, 4) == "Gate" then
print("Gate Detected")
local secondval = tonumber(string.sub(v.Name, 5))
print(secondval)
if secondval <= val
v:Destroy()
end
end
end
Doubt this will work without debugging it. But, use this and check for errors. Run it and tell me what prints out. Also, sorry for the formatting.
string.sub
Returns the substring of s that starts at i and continues until j. i and j can be negative. i defaults to 1 and j defaults to -j.
Basically, the script is checking if the first 4 letters of v.Name equals “Gate.” If it does, it continues the code, recognizing it as a gate. If the 5th letter in the string is less than or equal to to the gatesValue, it destroys v.
A potential issue here is that the code isn’t checking if the gateValue is greater, not equal to.
If you want to keep the number values in the gate name, what you should do is convert the last part of the name from a string into a number.
-- this line of code turns the last part of the string into a number
local gateValue = tonumber(v.Name:sub(5))
Once you’ve done that, you should have an easier time comparing the player’s value with the gates.
local playerGateVal = gatesValue.Value
for i, v in pairs(world:GetDescendants()) do
local isGate = v.Name:sub(1, 4) == "Gate"
if not isGate then
-- since we're looping through the entire world,
-- we need to check if the descendant is a gate.
-- in this case, this object is not a gate.
continue
end
-- this line of code turns the last part of the string into a number
local gateValue = tonumber(v.Name:sub(5))
if gateValue <= playerGateVal then
v:Destroy()
end
end