Setting new transparency server sided

Heya, I am currently trying to make the transparency of a Model visible and invisible by using a RemoteEvent, Client works fine, it’s just the server script. Currently I don’t understand it that good but I did read articles about it and researched it before making this topic. Any help is appreciated.

Local Script:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ContentProvider = game:GetService("ContentProvider")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local EquipAndUnequip = Instance.new("Animation")
EquipAndUnequip.AnimationId = "rbxassetid://5603593119"
ContentProvider:PreloadAsync({EquipAndUnequip})
local Track = Humanoid:LoadAnimation(EquipAndUnequip)
Track.Looped = false
local Debounce = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("Transparency")


UserInputService.InputBegan:Connect(function(Input, gameProcessed)
	local KeyCode = Input.KeyCode
	for _,i in pairs(Glove:GetChildren()) do
		if (i:IsA("MeshPart") or i:IsA("UnionOperation")) and i.Transparency ~= nil then
			if KeyCode == Enum.KeyCode.One then 
				Track:Play()
				i.Transparency = i.Transparency == 1 and 0 or 1
				RemoteEvent:FireServer()
				Track:Destroy()
			end
		end
	end
end)

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Glove = workspace:WaitForChild("Grievous_SWU").RightHand.Glove
local NewTransparency = ReplicatedStorage:WaitForChild("Transparency")
 

local function setTransparency(newTransparency)
	for _,i in pairs(Glove:GetChildren()) do
		if (i:IsA("MeshPart") or i:IsA("UnionOperation")) and i.Transparency ~= nil then
			i.Transparency = newTransparency
		end
	end
end
 

NewTransparency.OnServerEvent:Connect(setTransparency)
1 Like

–Problem
When firing the remote event from the client you did not put your “newTransparency” parameter.
–Solutions

  1. Replace “RemoteEvent:FireServer()” with “RemoteEvent:FireServer(1)”
  2. Remove the parameter from the event handler in the server script and use the same logic from your local script to set the transparency on the server.

Good luck!

Hm, it doesn’t seem to work. Maybe I am just doing it wrong. It does say “attempt to call a number value” in the Console. I did what you suggested and I replaced “RemoteEvent:FireServer()” with “RemoteEvent:FireServer(1)”. And then I removed the parameter in the Server Script and replaced it with “1”, this is what I understood from this.

1 Like

If you choose the first solution then don’t change the server script. By “Replace ‘RemoteEvent:FireServer()’ with ‘RemoteEvent:FireServer(1)’” I meant the local script. Sorry about being a little confusing.
–Coding stuff
When you fire a remote event it sends the event and a group of values (called a tuple) to the server. When you make an event handler on the server the system puts those values you send into the connected function. So by sending the server (1) when you run “RemoteEvent:FireServer(1)” it runs your function on the server with the variable newTransparency equal to 1.

I’ve been trying a few things now and it still doesn’t seem to work. 1 thing did change, it no longer sends the error message, infact, it sends nothing at all, no errors. The Transparency on the Server is not working. I can provide the code again if you’d wish.

–Plan B
Plan B then. This is a little bit better anyways. Because you’re not using your setTransparency function for any other transparencies, you can do this: remove the parameter, replace transparency code in server with the working transparency code from the client.
–Little Important Side Note That I Totally Forgot To Mention, Sorry :face_with_hand_over_mouth:
Roblox works with a server-client system. Basically, changes on the server are replicated(sent) to the client. So if you set the transparency on the server for example, the server will tell the client to also change the transparency. One detail, this may take about 300 milliseconds(Read the comments below for how to chose an option).
–Another side note about coding optimization
You might want to make the “if KeyCode == Enum.KeyCode.One then” check before the for in pairs loop because if you don’t do this then that loop will run every time an input happens(ex. every click, every key pressed)
Client Code

UserInputService.InputBegan:Connect(function(Input, gameProcessed)
	local KeyCode = Input.KeyCode
	[[This is just a way to make a check so that I dont have to indent
	stuff(hard to edit that on this). Basicly if the key code isn't one it
	stops the function
	]]
	if not KeyCode == Enum.KeyCode.One then return end
	for _,i in pairs(Glove:GetChildren()) do
		if (i:IsA("MeshPart") or i:IsA("UnionOperation")) and i.Transparency ~= nil then
			--took this out here so it runs the loop only when one is pressed
			--if KeyCode == Enum.KeyCode.One then 
				Track:Play()
				--Keep this line if you don't want that 0.6 second delay(seems like a tool you're making I would keep it)(0.6 bc it goes to the server then back to the client)
				i.Transparency = i.Transparency == 1 and 0 or 1
				--Because the parameter was removed below there is no reason to send that extra number
				RemoteEvent:FireServer()
				Track:Destroy()
			--end
		end
	end
end)

Server Code:

local function setTransparency()
	for _,i in pairs(Glove:GetChildren()) do
		if (i:IsA("MeshPart") or i:IsA("UnionOperation")) and i.Transparency ~= nil then
			--Old code
			--i.Transparency = newTransparency

			--New code
			i.Transparency = i.Transparency == 1 and 0 or 1
		end
	end
end
 

NewTransparency.OnServerEvent:Connect(setTransparency)

Now every Key I press that is not “1”, it toggles the animation and reacts to every Key input I make. Also if I do press 1, same thing happens, the Glove “flickers” (Goes invisible, then visible again really fast.) Nothing in the output aswell.

--Local Script--
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ContentProvider = game:GetService("ContentProvider")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local EquipAndUnequip = Instance.new("Animation")
EquipAndUnequip.AnimationId = "rbxassetid://5603593119"
ContentProvider:PreloadAsync({EquipAndUnequip})
local Track = Humanoid:LoadAnimation(EquipAndUnequip)
Track.Looped = false
local Debounce = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("Transparency")

UserInputService.InputBegan:Connect(function(Input, gameProcessed)
	local KeyCode = Input.KeyCode
	if KeyCode == Enum.KeyCode.One then
		--Dont see where Glove is defined(I assume its there bc it worked once(kinda))
		for _,i in pairs(Glove:GetChildren()) do
			if (i:IsA("MeshPart") or i:IsA("UnionOperation")) and i.Transparency ~= nil then
				Track:Play()
				i.Transparency = 1
				RemoteEvent:FireServer()
				Track:Destroy()
			end
		end
	end
end)
--Server Script--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Glove = workspace:WaitForChild("Grievous_SWU").RightHand.Glove --This isnt a very good way to get a tool besides for testing(I recommend putting this script inside a tool then use script.whereever1.whereever2)
local NewTransparency = ReplicatedStorage:WaitForChild("Transparency")

local function setTransparency(newTransparency)
	for _,i in pairs(Glove:GetChildren()) do
		if (i:IsA("MeshPart") or i:IsA("UnionOperation")) and i.Transparency ~= nil then
			i.Transparency = 1
		end
	end
end

NewTransparency.OnServerEvent:Connect(setTransparency)

I didn’t have time to read all of the comments, but I believe I know what your problem is.

When you call the server from a client via remote event, the first parameter it sends is the player that called and THEN whatever parameters you send through the event. Which means your “newTransparency” variable is actually the player that called.

Solution: Include the Player in the function declaration line.

local function setTransparency(player, newTransparency)
--Stuff
end

I noticed that while I was writing the plan b section. In that I suggested they remove the parameter and it still didn’t work. Seems like a logic error in my code.

1 Like

I coded a little example really fast. Everything seems to work. Here is the code:

--Remote Event named "RemoteEvent" in ReplicatedStorage
--Local Script in starter player scripts
local UserInputService = game:GetService("UserInputService")
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.One then
		print("1 Key pressed")
		for i, p in pairs(workspace:GetChildren()) do
			if (p:IsA("BasePart") or p:IsA("MeshPart")) and p.Transparency then
				p.Transparency = 1
				remoteEvent:FireServer()
			end
		end
	end
end)
--Script in ServerScriptService
local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
remoteEvent.OnServerEvent:Connect(function()
	for i, p in pairs(workspace:GetChildren()) do
		if (p:IsA("BasePart") or p:IsA("MeshPart")) and p.Transparency then
			p.Transparency = 1
		end
	end
end)

This is in a blank baseplate world. In my world when I press 1 all the basepart children of workspace disappear in the client and the server.

Okay I updated the script. Sorry about being a little confusing, this is my first time replying to a dev forum question. Hope this works!
–Summary

  • The function to play an animation was causing a delay
  • The event had a default value added to the front
  • Optimization but kinda unimportant but a good practice: organize checks based on how likely they are to fail.
--Local Script--
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ContentProvider = game:GetService("ContentProvider")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local EquipAndUnequip = Instance.new("Animation")
EquipAndUnequip.AnimationId = "rbxassetid://5603593119"
ContentProvider:PreloadAsync({EquipAndUnequip})
local Track = Humanoid:LoadAnimation(EquipAndUnequip)
Track.Looped = false
local Debounce = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("Transparency")
--If the tool is equipped
local isEquipped = false -- this is the initial state.

UserInputService.InputBegan:Connect(function(Input, gameProcessed)
	local KeyCode = Input.KeyCode
	if KeyCode == Enum.KeyCode.One then
		--Dont see where Glove is defined(I assume its there bc it worked once(kinda))
		for _,i in pairs(Glove:GetChildren()) do
			if (i:IsA("MeshPart") or i:IsA("UnionOperation")) and i.Transparency ~= nil then
				
				if isEquipped then
					i.Transparency = 1
					RemoteEvent:FireServer(1)
				else
					i.Transparency = 0
					RemoteEvent:FireServer(0)
				end
				
				Track:Play()--Moved this bc this is prob the problem (caused a delay that made the server change first then the client change it back really fast)
				Track:Destroy()
			end
		end
	end
end)
--Server Script--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Glove = workspace:WaitForChild("Grievous_SWU").RightHand.Glove --This isnt a very good way to get a tool besides for testing(I recommend putting this script inside a tool then use script.whereever1.whereever2)
local NewTransparency = ReplicatedStorage:WaitForChild("Transparency")

local function setTransparency(plr, newTransparency)
	for _,i in pairs(Glove:GetChildren()) do
		if (i:IsA("MeshPart") or i:IsA("UnionOperation")) and i.Transparency ~= nil then
			i.Transparency = newTransparency
		end
	end
end

NewTransparency.OnServerEvent:Connect(setTransparency)

Here is the example place I made: Disappear Example.rbxl (20.2 KB)

Don’t worry about it. You’re doing an excellent job, with every failure victory comes closer and closer. Anyway, not to get off-topic, I tested the newest script you just provided, it doesn’t actually go transparant if I press “1”, for when I use the other script you posted before it did work.
I don’t know if this helps or not. I am trying to give as much detail as I can in 1 message, aswell as making the message itself clear to what I am trying to achieve with this script.

Opps I forgot to set the state variable. Its a really quick fix sorry about that. Add these lines of code to set it:

if isEquipped then
	isEquipped = false
	i.Transparency = 1
	RemoteEvent:FireServer(1)
else
	isEquipped = true
	i.Transparency = 0
	RemoteEvent:FireServer(0)
end

I’m actually gonna provide you a gif this time. It works for the half part, I am sorry if I take up a lot of your time, I didn’t think this would take this long aswell. But I appreciate the support and effort!

Alright, I have solved the issue. I kept putting the Server Script in SSS, now I put it in the model itself within ReplicatedStorage. Now everything works. I am sorry for the trouble and the time consumed. Like I said I am fairly new to Server Scripts. Thank you for the help!

Lol spoke too soon. It won’t go back to it’s original transparency. RIP.

I put some prints in the script and it seems to only print “1”, and not doing anything else. Here is the code:

UserInputService.InputBegan:Connect(function(Input, gameProcessed)
	local KeyCode = Input.KeyCode
	if KeyCode == Enum.KeyCode.One then
		for _,i in pairs(Glove:GetChildren()) do
			if (i:IsA("MeshPart") or i:IsA("UnionOperation")) and i.Transparency ~= nil then
				
				if not isEquipped then
					isEquipped = false
					i.Transparency = 1
					RemoteEvent:FireServer(1)
					print("1")
				else
					isEquipped = true
					i.Transparency = 0
					RemoteEvent:FireServer(0)
					print("0")
				end
				
				Track:Play()
				Track:Destroy()
			end
		end
	end
end)

Everytime I press 1, it seems to only print 1 and changing its transparency back to 0. I hope this is some useful information.

I think the reason why that happens is because it checks if isEquipped is false, then changes the value to false instead of true.

If I change the Value from False to True, it will print both “1” and “0”. This is how it looks in the console.