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

Script completely stops working after death?

Asked by 5 years ago

Hey everyone. I have been trying to find the reason behind why this script doesn't work once the player dies. The specific parts I have found that do not work are lines 58 - 70 and 233 - 242. Any help would be appreciated.

repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character

local RegPlacement = require(script.RegularPlacement)

local RepStorage = game:GetService("ReplicatedStorage")
local Modules = RepStorage.Modules

local PlaceFunction = RepStorage:FindFirstChild("PlaceFunction")

local Objects = RepStorage:FindFirstChild("Objects")
local Zones = RepStorage:FindFirstChild("ZoneTemplates")

local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()

local Data = Plr:WaitForChild("Data")
local Money = Data:WaitForChild("Money")
local Bux = Data:WaitForChild("Bux")

local PlrGui = Plr:WaitForChild("PlayerGui")

local MainGUI = PlrGui:WaitForChild("Main")

local Placing = false

----------

local CurrentItemSelection = nil

Plr.PlayerGui.Main.ResetOnSpawn = false

local Inventory = Plr:WaitForChild("Inventory")

local InvGUI = MainGUI:WaitForChild("Inventory")
local InvGUIButton = MainGUI:WaitForChild("InvButton")
local Info = InvGUI:WaitForChild("InventoryInfo")

local MainInv = InvGUI:WaitForChild("MainInv")
local MainInvFrame = MainInv:WaitForChild("Frame")
local SampleItem = InvGUI:WaitForChild("SAMPLEITEM")

local PlaceButton = InvGUI:WaitForChild("Place")
local SellButton = InvGUI:WaitForChild("Sell")
--local CloseButton = InvGUI.Sell

local StatsModule = require(Modules.ItemData)





----------

RegularPlacement = RegPlacement.new(workspace.TycoonPlate.PrimaryPart, workspace.Tycoon, 1)

--------------------

InvGUIButton.MouseButton1Click:Connect(function()

    print("Clack")  

    if InvGUI.Visible == false then
        RegularPlacement:Disable()
        InvGUI.Visible = true
    else
        InvGUI.Visible = false
    end


end)

game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").Died:Connect(function()

    RegularPlacement:Disable()

    wait(1)

    game.Players.LocalPlayer:LoadCharacter()

    wait(1)

    RefreshItems()

end)

---------------------------------------------------------------------

function ChooseObj(ModelName) -- Choose random object from a set of objects

    local RanNum = math.random(1, #Objects[ModelName]:GetChildren())

    if Objects[ModelName]:IsA("Folder") then
        for i, Type in pairs(Objects[ModelName]:GetChildren()) do

            if RanNum == i then
                return Type:Clone(), i
            end

        end
    else 
        return Objects[ModelName] 
    end

end

local function Clicked(ModelName) -- Initiate placement

    local newObj
    local Index
    local Disable = false

    if Objects:FindFirstChild(ModelName) and not Placing and Inventory:FindFirstChild(ModelName) then

        if Inventory[ModelName].Value == 1 then
            Disable = true
        end

        RegularPlacement:Disable()  

        InvGUI.Visible = false

        newObj, Index = ChooseObj(ModelName)

        if newObj:FindFirstChild("Selection") then
            newObj.Selection.Adornee = newObj.PrimaryPart
        end

        local Placement = RegularPlacement:Enable(newObj)

        Placement:Connect(function(String, Name, Pos, Rot)

            --RegularPlacement:Disable()

            Placing = true

            PlaceFunction:InvokeServer(String, Name, Index, Pos, Rot)
            RegularPlacement:Disable()

            Placing = false 

            wait(.25)

            if Disable then
                RegularPlacement:Disable()
            else
                Clicked(ModelName)
            end

        end)    
    else    
        RegularPlacement:Disable()  
    end 



end

function KeyPress(Key)
    if Key.KeyCode == Enum.KeyCode.X then
        RegularPlacement:Disable()
    end
end

game:GetService("UserInputService").InputBegan:Connect(KeyPress)



-----------------------------------

function ToTime(Seconds)

    if Seconds < 60 then
        return Seconds.."s"
    else
        local Min = math.floor(Seconds / 60)
        local Sec = Seconds % 60

        print("Min: "..Min, "Sec: "..Sec)
        return Min.."Min(s) "..Sec.." Seconds"
    end

end

function AddItem(Item)

    local InvItem = Inventory:FindFirstChild(Item.Name)

    if InvItem then

        local Stats = StatsModule.ItemData[InvItem.Name]

        local newButton = SampleItem:Clone()
        newButton.Name = Item.Name
        newButton.Parent = MainInvFrame
        newButton.Visible = true

        local Amt = newButton:WaitForChild("Amount")

        Amt.Text = tostring(InvItem.Value).."x"


        newButton.MouseButton1Click:Connect(function()

            CurrentItemSelection = InvItem

            Info:WaitForChild("ItemName").Text = InvItem.Name
            Info:WaitForChild("Description").Text = Stats["Description"]
            Info:WaitForChild("Type").Text = Stats["Type"]
            Info:WaitForChild("Production").Text = "Produces: ".."$"..Stats["Production"].."/sec"
            Info:WaitForChild("BuildTime").Text = "Build Time: "..ToTime(Stats["BuildTime"]) -- BuildTime is in seconds

        end)

        Inventory[Item.Name].Changed:Connect(function()

            Amt.Text = tostring(InvItem.Value).."x"

        end)

    end
end

PlaceButton.MouseButton1Click:Connect(function()

    if CurrentItemSelection then

        Clicked(tostring(CurrentItemSelection))

    end

end)

function RefreshItems()

    for Index, Item in pairs(Inventory:GetChildren()) do
        if not MainInvFrame:FindFirstChild(Item.Name) then
            AddItem(Item)
        end
    end


end

Inventory.ChildAdded:Connect(function(Item)

    print("New Item: "..Item.Name)

    AddItem(Item)

end)

Inventory.ChildRemoved:Connect(function(Item)

    print("Removing Item!")

    if MainInvFrame:FindFirstChild(Item.Name) then
        MainInvFrame[Item.Name]:Destroy()
    end

end)
0
Is this a localscript? If its a local script then once the player died, the script is removed. Troxure 87 — 5y
0
Yes it is in a local script. Is there any way I can have it "re-run" or something so it still works afterwards? coolepicjoshua 110 — 5y
0
I'm assuming the issue is with the gui. Have you tried making the gui property ResetOnSpawn = false? F_F 53 — 5y
0
Yes I have experimented with it. right now I have ResetOnSpawn set to false on line 30. coolepicjoshua 110 — 5y
0
i dont think anyone is gonna read this FireyMcBlox 134 — 5y

1 answer

Log in to vote
1
Answered by
Troxure 87
5 years ago

You can set the localscript nil so it still exists until the player leaves, or you can have a server script clone into their character,backpack,etc when they die and their character loads.

Ad

Answer this question