Hello
I have a question for you.
I can somehow reduce or modify this script.
Roblox only accepts my first line at DESTROY
game.ReplicatedStorage.PurchaseEvents[“Keycard Level 4”].OnServerEvent:Connect(function(Player)
if Player:WaitForChild(Player.Name…"'s Cash").Cash.Value >= 400 then
Player:WaitForChild(Player.Name…"'s Cash").Cash.Value = Player:WaitForChild(Player.Name…"'s Cash").Cash.Value - 400
local clone = game.ServerStorage.Tools["Keycard Level 4"]:Clone()
clone.Parent = Player.Backpack
Player.Backpack["Keycard Level 1"]:Destroy()
Player.Backpack["Keycard Level 2"]:Destroy()
Player.Backpack["Keycard Level 3"]:Destroy()
return
end
end)
Before Thanks
Codycheck
1 Like
I went ahead and added an additional function that checks for all the current Keycards you have in your Inventory, and will automatically Destroy them if they’re valid or not
local RepStorage = game:GetService("ReplicatedStorage")
local PurchaseEvents = RepStorage:WaitForChild("PurchaseEvents")
local function CheckKeycards(Plr, Child)
local Char = Plr.Character
local StarterGear = Plr:WaitForChild("StarterGear")
if Char and Char:FindFirstChild(Child) then
Char[Child]:Destroy()
else
Plr.Backpack[Child]:Destroy()
end
if StarterGear:FindFirstChild(Child) then
StarterGear[Child]:Destroy()
end
end
PurchaseEvents["Keycard Level 4"].OnServerEvent:Connect(function(Player)
local Cash = Player:WaitForChild(Player.Name.."'s Cash")
if Cash.Cash.Value >= 400 then
Cash.Cash.Value -= 400
end
for LevelNumber = 1, 3 do
CheckKeycards(Player, "Keycard Level "..LevelNumber)
end
end)
The main issue why your script stops at the first Destroy() function is that you’re assuming that the Player already has the Level 2, and Level 3 Keycards as well but since they don’t, it results at an error & stops working
You have to first check if they have it or not by using FindFirstChild(), which either returns back the Instance, or nil if it’s unable to be found
1 Like
Thanks for answer.
Script stop, first line and when „PLAYER“ have Keycards Level 2. I thought so.
I thought that if he couldn’t find something, the script went on normally
My normal jobs is modeling in 3ds max. Sorry im not strong lua script and in UE4 use visual scripting and BASIC before 30 years 
I must now, this script move in between two scripts my project.
Hello
When I take CARD 1, after CARD in between 2 to 9 is it allready, but take high card, system not destroyed child CARD
more videolink
https://cody.cz/Data/Karta-problem-01.mp4
game.ReplicatedStorage.PurchaseEvents[“Keycard Level 3”].OnServerEvent:Connect(function(Player)
if Player:WaitForChild(Player.Name…"'s Cash").Cash.Value >= 300 then
Player:WaitForChild(Player.Name…"'s Cash").Cash.Value = Player:WaitForChild(Player.Name…"'s Cash").Cash.Value - 300
local clone = game.ServerStorage.Tools["Keycard Level 3"]:Clone()
clone.Parent = Player.Backpack
wait(2)
local function Trojka(Plr, Child)
local Char = Plr.Character
local StarterGear = Plr:WaitForChild("Backpack")
if Char and Char:FindFirstChild(Child) then
Char[Child]:Destroy()
else
Plr.Backpack[Child]:Destroy()
end
if StarterGear:FindFirstChild(Child) then
StarterGear[Child]:Destroy()
end
end
for jednadva = 1, 2 do
Trojka(Player, "Keycard Level "..jednadva)
end
return
end
end)
Before Thanks
I can’t really understand what you’re saying, but I’m assuming you’re wanting to delete an old Keycard if you’re attempting to purchase a Higher Level then that
There are 2 ways you can go about this:
-
Destroy any keycards that have the name “Keycard” within your Backpack or Player’s Character to grant you an entire new Keycard
-
Only destroy the Keycard that’s a lower level then the Keycard you currently have, and replace it with the Higher Level one
Refer to the script I sent you again, you could just instead simply increase the max interval in the loop by the current amount of Keycards you currently have, or use string.find() to check for the string “Keycard” in the Player’s Backpack or Character
local RepStorage = game:GetService("ReplicatedStorage")
local PurchaseEvents = RepStorage:WaitForChild("PurchaseEvents")
local function CheckKeycards(Plr)
local Char = Plr.Character
local StarterGear = Plr:WaitForChild("StarterGear")
local Backpack = Plr:WaitForChild("Backpack")
for _, Keycard in pairs(StarterGear:GetChildren()) do
if Keycard:IsA("Tool") and string.find(tostring(Keycard), "Keycard") then
Keycard:Destroy()
end
end
for _, Keycard in pairs(Backpack:GetChildren()) do
if Keycard:IsA("Tool") and string.find(tostring(Keycard), "Keycard") then
Keycard:Destroy()
end
end
if Char then
for _, Keycard in pairs(Character:GetChildren()) do
if Keycard:IsA("Tool") and string.find(tostring(Keycard), "Keycard") then
Keycard:Destroy()
end
end
end
end
PurchaseEvents["Keycard Level 4"].OnServerEvent:Connect(function(Player)
local Cash = Player:WaitForChild(Player.Name.."'s Cash")
if Cash.Cash.Value >= 400 then
Cash.Cash.Value -= 400
end
CheckKeycards(Player)
end)