Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

attempt to index nil with 'Parent'???(help)

Asked by 3 years ago

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)
0
Which line has the error User#30567 0 — 3y
0
line 5 Lamborghinihurican0 4 — 3y
0
What is "cases"? I figured out the problem, just need to know more before i answer User#30567 0 — 3y
0
on line 4 it goes to (function(cases) Lamborghinihurican0 4 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

(probably not going to help you answer your question)

I am going to rewrite this whole script. I will split it into three parts.

1. Client script

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)

2. Replicated event

You will need to create a replicated event in order for this to work

  1. Scroll to ReplicatedStorage in explorer
  2. Insert a RemoteEvent in ReplicatedStorage
  3. Rename the RemoteEvent to RollEvent

If you don't follow the above instructions, your code will break

3. Server script

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

0
what type of script should i use Lamborghinihurican0 4 — 3y
0
it says fire is not a valid member of remote event Lamborghinihurican0 4 — 3y
Ad

Answer this question