I am trying to make a randomizing case script that gives the random item to the player.. but it says in the output attempt to index nil with 'Parent' and I dont know what it means
here is the script...
local commoncase = game:WaitForChild("Workspace").CommonCase local b = script.Parent b.MouseButton1Click:Connect(function(cases) if cases.Parent.Humanoid~= nil then local cash = game.Players:findFirstChild(cases.Parent.Name).leaderstats.Cash if cash.Value == cash.Value > 100 then local chi = math.random(1,10) if chi == 1 then local clone = game.Workspace.CommonCase.Common:WaitForChild("Sabre"):clone() clone.Parent = game.Players.LocalPlayer.Backpack end if chi == 10 then local clone = game.Workspace.CommonCase.Epic:WaitForChild("hyper"):clone() clone.Parent = game.Players.LocalPlayer.Backpack end if chi == 2 then local clone = game.Workspace.CommonCase.Common:WaitForChild("Axe"):clone() clone.Parent = game.Players.LocalPlayer.Backpack end if chi == 9 then local clone = game.Workspace.CommonCase.Rare:WaitForChild("ChristmasTreeSword"):clone() clone.Parent = game.Players.LocalPlayer.Backpack end if chi == 3 then local clone = game.Workspace.CommonCase.Common:WaitForChild("Crowbar"):clone() clone.Parent = game.Players.LocalPlayer.Backpack end if chi == 8 then local clone = game.Workspace.CommonCase.Common:WaitForChild("Axe2"):clone() clone.Parent = game.Players.LocalPlayer.Backpack end if chi == 4 then local clone = game.Workspace.CommonCase.Common:WaitForChild("Fife"):clone() clone.Parent = game.Players.LocalPlayer.Backpack end if chi == 7 then local clone = game.Workspace.CommonCase.Common:WaitForChild("Combat knife"):clone() clone.Parent = game.Players.LocalPlayer.Backpack end if chi == 5 then local clone = game.Workspace.CommonCase.Common:WaitForChild("Machete"):clone() clone.Parent = game.Players.LocalPlayer.Backpack end if chi == 6 then local clone = game.Workspace.CommonCase.Common:WaitForChild("Lance") clone.Parent = game.Players.LocalPlayer.Backpack end end end end)
(probably not going to help you answer your question)
I am going to rewrite this whole script. I will split it into three parts.
This will be the client script
local RS = game:GetService("ReplicatedStorage") local RollEvent = RS:WaitForChild("RollEvent") -- Wait for event local RollButton = script.Parent -- Put this script inside the button RollButton.Activated:Connect(function() RollEvent:FireServer("Common") end)
You will need to create a replicated event in order for this to work
ReplicatedStorage
in explorerRemoteEvent
in ReplicatedStorage
RemoteEvent
to RollEvent
If you don't follow the above instructions, your code will break
This will be the server script
--[[ Case script by TTChaos ["Item Name"] = {Location = Location, Rarity = 50}, Rarity is optional, Location is necessary --]] local CommonCase = workspace.CommonCase -- Common Case Location local RS = game:GetService("ReplicatedStorage") local RollEvent = RS:WaitForChild("RollEvent") local CommonCaseItems = { ["Sabre"] = {Location = CommonCase.Common.Sabre}, ["Axe"] = {Location = CommonCase.Common.Axe}, ["Crowbar"] = {Location = CommonCase.Common.Crowbar}, ["Axe 2"] = {Location = CommonCase.Common.Axe2}, -- Name it something better ["Fife"] = {Location = CommonCase.Common.Fife}, ["Combat Knife"] = {Location = CommonCase.Common["Combat Knife"]}, ["Machete"] = {Location = CommonCase.Common.Machete}, ["Lance"] = {Location = CommonCase.Common.Lance}, ["Chistmas Tree Sword"] = {Location = CommonCase.Rare.ChristmasTreeSword}, ["Hyper"] = {Location = CommonCase.Epic.Hyper}, } local RareCaseItems = { -- Example ["Christmas Tree Sword"] = {Location = CommonCase.Rare.ChristmasTreeSword} } function DetectRarity(Player, Rarity) -- Define the function if Rarity == "Common" then if PlayerCanAfford(100, Player) then RollCase(Player, CommonCaseItems) -- Roll case end elseif Rarity == "Rare" then if PlayerCanAfford(250, Player) then RollCase(Player, RareCaseItems) -- Example end end print("Finished!\n") end --[[ Please do not change anything below this comment Changing anything below this comment will break the script --]] function PlayerCanAfford(Price, Player) -- Function to deduct cash if Player.leaderstats.Cash.Value >= Price then Player.leaderstats.Cash.Value -= Price return true else return false end end function RollCase(Player, CaseRarity) local Counter = 0 -- Roll a random number between 1 and 100 local RandomChoice = math.random(1, 100) for ItemName, ItemProperties in pairs(CaseRarity) do -- Loop through items local ItemRarity = ItemProperties["Rarity"] or 100/#CaseRarity -- Assign rarity if (RandomChoice > Counter) and (RandomChoice <= Counter + ItemRarity) then -- Detect if Item was rolled print(string.format("You got a %s! The chances of getting one is %u%%!", ItemName, ItemRarity)) AwardItem(Player, ItemName, ItemProperties["Location"]) else print(string.format("No, the item is not a %s.", ItemName)) end Counter = Counter + ItemRarity end end function AwardItem(Player, ItemName, Location) -- Simple award script local ItemClone = Location:Clone() ItemClone.Parent = Player.Backpack end RollEvent.onServerEvent:Connect(DetectRarity)
Edit: Doesn't deduct the coins