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

Why doesn't player get visible again?

Asked by
Oficcer_F 207 Moderation Voter
5 years ago

When the tool is equipped, it will make the player invisible and invincible, when it is unequipped again, it will make the player visible again. But, the player doesn't turn normal again after unequipping the tool.

script.Invincible.OnServerEvent:Connect(function(player, tool)
    local function makeInvisible()

    for i,v in pairs(player.Character:GetChildren()) do
        if v.ClassName == "MeshPart" or v.ClassName == "Part" then
            v.Transparency = 0.5
        end

    end


end

    player.Character.Humanoid.MaxHealth = math.huge
    player.Character.Humanoid.Health = math.huge
    player.Character.HumanoidRootPart.Transparency = 1
    makeInvisible()
end)

script.RemoveInvincible.OnServerEvent:Connect(function(player, tool)
        local function makeVisible()
            for i,v in pairs(player.Character:GetChildren()) do
        if v.ClassName == "MeshPart" or v.ClassName == "Part" then
            v.Transparency = 0
        end

    end

    player.Character.Humanoid.MaxHealth = 100
    player.Character.Humanoid.Health = 100
    makeVisible()

    end
end)
0
Where's the localscript? So we see what's going on :D mixgingengerina10 223 — 5y
0
Are you sure the RemoveInvincible event gets fired? Try adding a print at the top of its handler Amiaa16 3227 — 5y

1 answer

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

Side note: please indent your code properly, it was extremely hard to find your problem.

The problem is very simple. You were recursively calling the function, which is valid, but you didn't call the function itself. Simply add a call to makeInvisible and makeInvisible, and problem solved.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function makeInvisible(player)
    for i,v in pairs(player.Character:GetChildren()) do
        if v:IsA("BasePart") then -- use inheritance! the Part and MeshPart class inherit BasePart
            v.Transparency = 0.5
        end
    end
    player.Character.Humanoid.MaxHealth = math.huge
    player.Character.Humanoid.Health = math.huge
end

local function makeVisible(player)
    for i,v in pairs(player.Character:GetChildren()) do
        if v:IsA("BasePart") then
            v.Transparency = 0
        end
    end
    player.Character.Humanoid.MaxHealth = 100
    player.Character.Humanoid.Health = 100
end

-- You should put the remotes in ReplicatedStorage
ReplicatedStorage.Invincible.OnServerEvent:Connect(makeInvisible)
ReplicatedStorage.RemoveInvincible.OnServerEvent:Connect(makeVisible)
-- Connect the functions! They are called when the event is fired.
0
Wow. Interesting. mixgingengerina10 223 — 5y
Ad

Answer this question