Hello,
So I am making a button that turns the player invisible (And another that turns them visible again.)
Both of the scripts seemed to be working great until I realised they were only working locally, so the player is still visible for everyone else.
I’m not too savvy with global scrirpts like this, could someone please help me out?
Thank you!
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
script.Parent.MouseButton1Click:Connect(function()
for _, part in ipairs(Character:GetDescendants()) do
if part:IsA('BasePart') or part:IsA('Decal') then
part.Transparency = 1
end
end
end)
In a server script, the first parameter of a ClickDetector event will return as a Player object.
script.Parent.MouseButton1Click:Connect(function(player) --"player" is the player who clicked the button
for _, part in ipairs(player.Character:GetDescendants()) do
if part:IsA("Part") or part:IsA("Decal") then
part.Transparency = 1
end
end
end)
The code works fine for me. Are you trying to do the function before the player’s character is fully loaded? Also, try making the code be run on the server and not a local script.
If the character not being loaded yet is the problem, insert this line of code after script.Parent.MouseButton1Click:Connect(function():
if not Character then warn("Character not loaded!") return end
If this is being done within the LocalScript of a GUI, and you wish for all players to be able to see the invisibility, it’s recommended to use RemoteEvents; client-sided scripts (i.e., the LocalScript) will only effect things under the client it belongs to, so won’t be visible to everyone unless the LocalScript calls on a server script
Essentially, for it to work properly, the actual invisibility code will be done inside a server script, and the local script will call upon a RemoteEvent tied to said server script to activate the invisibility
Here are some articles more helpful than myself: