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

How can I remove a player's ownership of a tycoon when they die, not leave?

Asked by 7 years ago

I've been working on my game which is both a team war and tycoon game. However, as I have gotten a little better at scripting, I have started to experiment with different things. One of them is the tycoons of my game. When the owner of the tycoon dies, not leave the game, but dies, I want the tycoon to regenerate to a freshly new one where someone else has the chance to gain ownership of it.

Here I have provided the script and some more info:

local ting = 0 --debouncer
local owner = script.Parent.Parent.Parent.OwnerName --This is the thing that holds the owners name
local ownerhealth = script.Parent.Parent.Parent.OwnerHealth -- OwnerHealth is in the same model of OwnerName
function onTouched(hit) -- When someone hits this door do the following.
    if ting == 0 then --debounce check
    ting = 1 --activate debounce
    local check = hit.Parent:FindFirstChild("Humanoid") --Find the human that touched the button
        if check ~= nil then --If a human is found, then do the following.
            local thisplr = game.Players:findFirstChild(hit.Parent.Name) -- Checks humans name.
            local thisplrhealth = game.Players:findFirstChild(hit.Parent.Humanoid.Health)           
            if (thisplr~=nil) then -- If humans name is found, then do the  following.
            if (thisplrhealth~=nil) then -- If the human's health is found, same thing as above.
                local ownstyc = thisplr:findFirstChild("Tycoon") -- Checks player to see if he has the Tycoon value.
                if (ownstyc~=nil) then -- If the tycoon value is found on the player, do the following.
                    if ownstyc.Value == 0 then -- If the tycoon value is 0 then do the following.
                        ownstyc.Value = 1  -- Make the tycoon value 1.
                        owner.Value = thisplr.Name -- Player's value is now the Owner's value.
                        ownerhealth.Value = thisplrhealth.Value -- The health has been recorded by OwnerHealth.
                        local message = Instance.new("Message") -- Types a message to the player.
                        message.Text = "You Now own a Tycoon" -- Message...
                        message.Parent = thisplr -- Makes sure that the message goes to the player.
                        wait(3) -- Wait 3 seconds.
                        message:remove() -- Remove message.
                        script.Parent.Parent:remove() -- Removes door.
                    elseif ownstyc.Value == 1 then -- If the players Tycoon Value is 1.
                        local message = Instance.new("Message") -- Types a message to the player.
                        message.Text = "You Already own a Tycoon" -- Message...
                        message.Parent = thisplr -- Makes sure the message goes to the player.
                        wait(3) -- Wait 3 seconds.
                        message:remove() -- Remove message.
                    end -- End process.
                end -- End process.
            end -- End process.
        end -- End process.
    ting = 0 --remove debounce
    end -- End process.
end -- End process.
end
script.Parent.Touched:connect(onTouched) --Start listening for button-touchers.

Basically, I created a value called "ownerhealth" as you can see at the very top of the script. It's supposed to follow through everything just like "owner", except "owner" checks the owner of the tycoon's name. "ownerhealth" of course checks the owner of the tycoon's health. I did find that there is something wrong with the checking of the owner's health. When I went into Play Solo, I touched the tycoon's "Become Owner" part which then turned me into the tycoon's owner. When I checked the Value "OwnerName", it showed my name, but when I checked the Value "OwnerHealth", it was blank. "local owner" effects the Value "OwnerName" while "local ownerhealth" is SUPPOSED to effect "OwnerHealth" which are located together inside of a model called Factory. Factory consists of every model of the tycoon except for the regenerating scripts that normally make the tycoon a fresh start, but ONLY when the player leaves the game. Once again, I want to make the tycoon regenerate into a fresh start when the player dies, not leaves.

1 answer

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

when the Player claims the tycoon. somewhere within that process add:

player.CharacterAdded:connect(function(char)
    --remove ownership/reset tycoon
end)

Complete Script if needed:

local ting = 0
local owner = script.Parent.Parent.Parent.OwnerName
local ownerhealth = script.Parent.Parent.Parent.OwnerHealth
function onTouched(hit)
    if ting == 0 then
    ting = 1
    local check = hit.Parent:FindFirstChild("Humanoid")
        if check ~= nil then
            local thisplr = game.Players:findFirstChild(hit.Parent.Name)        
            if (thisplr~=nil) then
                local ownstyc = thisplr:findFirstChild("Tycoon")
                if (ownstyc~=nil) then
                    if ownstyc.Value == 0 then
                        ownstyc.Value = 1
                        owner.Value = thisplr.Name
                        thisplr.CharacterAdded:connect(function(char)
                            --reset the tycoon
                            ownstyc.Value = 0
                        end)
                        local message = Instance.new("Message")
                        message.Text = "You Now own a Tycoon" 
                        message.Parent = thisplr
                        wait(3)
                        message:remove()
                        script.Parent.Parent:remove()
                    elseif ownstyc.Value == 1 then 
                        local message = Instance.new("Message")
                        message.Text = "You Already own a Tycoon"
                        message.Parent = thisplr
                        wait(3)
                        message:remove()
                    end
                end
            end
        ting = 0
        end 
    end
end
script.Parent.Touched:connect(onTouched)

Ad

Answer this question