You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
I need my script to return the item that shares the same name as the string passed through the function
- What is the issue? Include screenshots / videos if possible!
For some reason this script only works like 10% of the time when I hit the play button, and I have 0 idea why.
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried adding wait tasks to see if some things were being added late / in the wrong order, but I cant replicate a state where its working 100% or 0% of the time
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local BuyFunc = game.ReplicatedStorage:WaitForChild("BuyFunc")
local UpdateUI = game.ReplicatedStorage:WaitForChild("UpdateUI")
local brokieSound = script.Fail
local MoneSound = script.Sucsess
local itemsFolder = script.Items
------------------------------------ Define some basline variables ^^^^^^^^^
local function GiveItem(NameOfItem)
--scan throught the items folder looking for the item that shares a name with "Name of item"
for i,v in pairs(itemsFolder:GetChildren()) do
if v.Name == NameOfItem then
--if we find it, clone it into the player's backpack
return v
end
end
end
----------------------^^^^^^ Sorts through a folder looking for an item that is equal to the input thats passed through (Which is "Name of item"), but for SOME REASON it skips the first item in the folder, resulting in nothing being returned
BuyFunc.OnServerEvent:Connect(function(player,ActiveShopTab,ActicveCurrentButton,ActivePrice,ActiveName,SBuyType)
local leaderstats = player:FindFirstChild('leaderstats') or Instance.new("Folder", player)
local Credits = leaderstats:WaitForChild("Credits ¢")
local RollLim = leaderstats:WaitForChild("Roll Limit")
--^^^^^^^^^^^^^^ Defines leaderstats, Things like cash and the roll limit of a player
print("=+=+=+= BUY TYPE -- "..SBuyType.Value.." -- BUY TYPE =+=+=+=")
-- Debug printing ^^
if Credits.Value <= ActivePrice then
print("Ha broke")
brokieSound:Play()
return
end
--Checks the money ^^
if ActiveName == "Roll Limit Upgrade" then
RollLim.Value = RollLim.Value * 1.01
end
--^^Used for the roll limit upgrade
if SBuyType.Value == "Item" then
print("Looking for "..ActiveName)
print(GiveItem(ActiveName))
--^Returns Nil / nothing at all like 50% of the time^
local itemClone = GiveItem(ActiveName):Clone()
--^Crashes Script^
itemClone.Parent = player.Backpack
end
Credits.Value = Credits.Value - ActivePrice
MoneSound:Play()
end)