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

Why Does The Buyer Die Walking Into His Home When He Is The Owner And Has Already Died?

Asked by
jacobwow 140
10 years ago

I have a buy a house with gold script. If you own the house and walk in it heals you, but if you are not the owner you die. However I have a problem, when the buyer dies and walks into his home, it kills him. How can I fix this? The Script:

local Button = script.Parent
local debounce = true
local HasAccess = nil

function onTouch(Brick)
    if debounce == true then
    local Player = Brick.Parent:findFirstChild("Humanoid")
    if (Player ~= nil) then
        local Location = game:GetService('Players'):GetPlayerFromCharacter(Player.Parent)
    if Location:WaitForChild("leaderstats").Gold.Value >= 20 then
for i = 1,1 do
         Location:WaitForChild("leaderstats").Gold.Value  =  Location:WaitForChild("leaderstats").Gold.Value - 20
        script.Parent.Parent.Name = Player.Parent.Name .. "'s House"
        HasAccess = Player
        local Color1 = BrickColor:Random()
        local Color2 = BrickColor:Random()

        script.Parent.Parent.Wall1.BrickColor = Color1
        script.Parent.Parent.Wall2.BrickColor = Color1
        script.Parent.Parent.Wall3.BrickColor = Color1

        script.Parent.Parent.Floor.BrickColor = Color2
        script.Parent.Parent.Head.BrickColor = Color2
        script.Parent.Parent.Roof.BrickColor = Color2
        script.Parent.CanCollide = false
        script.Parent.Transparency = .62
        debounce = false
                end
            end
        end
    end
end

Button.Touched:connect(onTouch)

function Vip(Brick)
    if script.Parent.Parent.Name ~= "House For Sale - 20 Gold" then
    local Toucher = Brick.Parent:findFirstChild("Humanoid")
    if Toucher ~= nil then
        if Toucher == HasAccess then
            HasAccess.Health = HasAccess.MaxHealth
        else Toucher.Health = 0
            end
        end 
    end
end

Button.Touched:connect(Vip)

1 answer

Log in to vote
1
Answered by
MrFlimsy 345 Moderation Voter
10 years ago

The issue is that when the character dies and respawns, everything inside the character (including the Humanoid) is removed and re-created. When this happens, your variable gets reset. Instead of setting a variable to the Humanoid itself, you can try storing the player's name:

local Button = script.Parent
local debounce = true
local HasAccess = "N/A"

function onTouch(Brick)
    if debounce == true then
    local Player = Brick.Parent
    local Hum = Player:findFirstChild("Humanoid")
    if (Hum ~= nil) then
        local Location = game:GetService('Players'):GetPlayerFromCharacter(Player)
    if Location:WaitForChild("leaderstats").Gold.Value >= 20 then
for i = 1,1 do
         Location:WaitForChild("leaderstats").Gold.Value  =  Location:WaitForChild("leaderstats").Gold.Value - 20
        script.Parent.Parent.Name = Player.Name .. "'s House"
        HasAccess = Player.Name
        local Color1 = BrickColor:Random()
        local Color2 = BrickColor:Random()

        script.Parent.Parent.Wall1.BrickColor = Color1
        script.Parent.Parent.Wall2.BrickColor = Color1
        script.Parent.Parent.Wall3.BrickColor = Color1

        script.Parent.Parent.Floor.BrickColor = Color2
        script.Parent.Parent.Head.BrickColor = Color2
        script.Parent.Parent.Roof.BrickColor = Color2
        script.Parent.CanCollide = false
        script.Parent.Transparency = .62
        debounce = false
                end
            end
        end
    end
end

Button.Touched:connect(onTouch)

function Vip(Brick)
    if script.Parent.Parent.Name ~= "House For Sale - 20 Gold" then
    local Toucher = Brick.Parent
    local Hum = Toucher.Humanoid
    if Hum ~= nil then
        if Toucher.Name == HasAccess then
            Hum.Health = Hum.MaxHealth
        else Hum.Health = 0
            end
        end 
    end
end

Button.Touched:connect(Vip)
Ad

Answer this question