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

how do I make player dissapear button work on server side?

Asked by 2 years ago

I have this gui button that when clicked makes player dissapear but i just realised it only works on client side, how do i make player invisible on server side?

Btw this is my current code that works perfectly on client side:

local button = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local activated = false

local function dissapear()
    if activated == true then
        local descendants = character:GetDescendants()
        activated = false
        for i, descendant in pairs(descendants) do
            if descendant:IsA("BasePart") then
                descendant.Transparency = 0
                character.Head.face.Transparency = 0
                humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Viewer
                humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.DisplayWhenDamaged
            end
        end
    else
        local descendants = character:GetDescendants()
        activated = true
        for i, descendant in pairs(descendants) do
            if descendant:IsA("BasePart") then
                descendant.Transparency = 1
                character.Head.face.Transparency = 1
                humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
                humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOn
            end
        end
    end
end



local function onButtonActivated()
    if activated == false then
        dissapear()
        character.HumanoidRootPart.Transparency = 1
        activated = true
        button.BackgroundColor3 = Color3.new(0.133333, 1, 0)
    else
        dissapear()
        character.HumanoidRootPart.Transparency = 1
        activated = false
        button.BackgroundColor3 = Color3.new(0.6, 0.6, 0.6)
    end


end

button.Activated:Connect(onButtonActivated)


1 answer

Log in to vote
0
Answered by 2 years ago

Make a RemoteEvent and fire it when the button gets clicked, copy and paste the code on a server side script that runs when the RemoteEvent gets fired

it looks similar to something like this (its recommended to make RemoteEvents in ReplicatedStorage):

local RS = game:GetService("ReplicatedStorage")

local invisible = false
local button = script.Parent:FindFirstChild("TextButton")

button.MouseButton1Click:Connect(function()
    if not invisible then
        RS.invisibleEvent:FireServer("Invisible")
        invisible = true
    else
        RS.invisibleEvent:FireServer("Visible")
        invisible = false
    end
end)

^ that should cover the client side part

and now you detect the event in the server script.

local RS = game:GetService("ReplicatedStorage")

RS.invisibleEvent.OnServerEvent:Connect(function(player, iRemote)
    if iRemote == "Invisible" then
        -- code to make player invisible
    elseif iRemote == "Visible" then
        -- code to make player visible again
    end
end)

you can just switch up what I put here with your own code which does exactly what you're looking for.

0
Thanks kubapro213748 -13 — 2y
0
Wait but when i click the button everyone on the server gets invisible is there a way to check who clicked it? kubapro213748 -13 — 2y
0
the first keyword in the server script called 'player' tracks the localplayer ABDJackioZoAlt1 37 — 2y
Ad

Answer this question