Value is passed incorrectly from a local script to server script

I’m trying to pass a value from a local script (which is also set in a local script) to a script through remote event. The problem is it only passes it once and then the script stops receiving the right values. I tried looking this up but haven’t found anything.

Local Script:

local rs = game:GetService("ReplicatedStorage")
local folder = rs:WaitForChild("RemoteEvents")
local re = folder.PassPlayer
local re2 = folder.DoubleBlinker

local CAS = game:GetService("ContextActionService")
local player = game:GetService("Players").LocalPlayer

local ActionName = "BothBlinkers"

local cd = false
local cTime = 1

re.OnClientEvent:Connect(function(plr)
	local function handleLeftBlinker(actionName, inputState, inputObject)
		if actionName == ActionName and inputState == Enum.UserInputState.Begin then
			for i, v in pairs(game:GetService("Workspace"):GetDescendants()) do
				if v:IsA("VehicleSeat") and plr == player and string.match(v.Parent.Name, plr.UserId) then
					local car = v.Parent
					local isClicked = car:FindFirstChild("CheckBlinkers")
					
					if cd == false then
						cd = true
						if isClicked.Value == false then
							isClicked.Value = true
						else
							isClicked.Value = false
						end
						wait(0.5)
						re2:FireServer(isClicked.Value)
						
						wait(cTime)
						cd = false
					end
				end
			end		
		end
	end
	CAS:BindAction(ActionName, handleLeftBlinker, true, Enum.KeyCode.R)
	CAS:SetTitle(ActionName, "Both Blinkers")
	CAS:SetDescription(ActionName, "Engage Both Blinkers")
	CAS:SetPosition(ActionName, UDim2.new(0, 0, 0, 0))
end)

Script:

local rs = game:GetService("ReplicatedStorage")
local folder = rs:WaitForChild("RemoteEvents")
local re = folder.DoubleBlinker

local blinker1 = script.Parent.Parent.LBlinker
local blinker2 = script.Parent.Parent.RBlinker

re.OnServerEvent:Connect(function(player, isClicked)	
	while isClicked == true do
		blinker1.Material = Enum.Material.Neon
		blinker2.Material = Enum.Material.Neon
		task.wait(0.5)
		blinker1.Material = Enum.Material.SmoothPlastic
		blinker2.Material = Enum.Material.SmoothPlastic
		task.wait(0.5)
	end
end)
3 Likes

That’s because anything you do in the client doesn’t replicate to the server. For example, if you create a part in a local script, parent it to game.Workspace, do whatever with it, and send the part to a server script, the server script would interpret the part as nil, because nothing the client does replicates to the server.

Like @J_Angry said, values changed on the server don’t replicate to the client.

So it won’t know the value of isClicked.

I would recommend firing the event like this:

if isClicked.Value == false then
	isClicked.Value = true
	re2:FireServer(true)
else
	isClicked.Value = false
	re2:FireServer(false)
end

It finally passes the right Value but the server script

local rs = game:GetService("ReplicatedStorage")
local folder = rs:WaitForChild("RemoteEvents")
local re = folder.DoubleBlinker

local blinker1 = script.Parent.Parent.LBlinker
local blinker2 = script.Parent.Parent.RBlinker

re.OnServerEvent:Connect(function(player, isClicked)	
	while isClicked == true do
		blinker1.Material = Enum.Material.Neon
		blinker2.Material = Enum.Material.Neon
		task.wait(0.5)
		blinker1.Material = Enum.Material.SmoothPlastic
		blinker2.Material = Enum.Material.SmoothPlastic
		task.wait(0.5)
	end
end)

still doesn’t stop changing the material. When i’s already false (I checked with a print that the value is indeed false) which is weird? I know it might be off topic here but I wanna learn about why this doesn’t work and I don’t want to create a new topic just because of it.

Make the isClicked a local variable, because when the event fires all it does is replicate the function when it’s fired. So the loop is still running in the old event because the value isn’t actually changing it’s static, when you fire the event again it’ll create a new running instance of this function where the var is false.

Try this:

local blinker1 = script.Parent.Parent.LBlinker
local blinker2 = script.Parent.Parent.RBlinker
local isClicked = false

re.OnServerEvent:Connect(function(player, isClickedValue)	
    isClicked = isClickedValue
	while isClicked == true do
		blinker1.Material = Enum.Material.Neon
		blinker2.Material = Enum.Material.Neon
		task.wait(0.5)
		blinker1.Material = Enum.Material.SmoothPlastic
		blinker2.Material = Enum.Material.SmoothPlastic
		task.wait(0.5)
        if isClicked == false then
          break
        end
	end
end)

The problem with this is, when I then play it with more players, it changes the color on all of the models of my can I spawn in, meaning one player can enable the material change for every single player in the server who has the same car spawned. I’m sorry for not specifing this and sorry for using your time. :sweat_smile:
Off
On

Question can I see the hierarchy of your explorer, and where the parts are?

This is the bus model, it’s then cloned into workspace when a player spawns it
image

Wait, so does it happen if another player spawns a bus, or is it just when a player spawns two cars, both change?

It happens when another player spawns a bus. I have a script that doesn’t allow player to have 2 busses at the same time.

Hmm, okay. Oh I know, we can create a table… like such which will create an isClicked for each player.

local blinker1 = script.Parent.Parent.LBlinker
local blinker2 = script.Parent.Parent.RBlinker
local ClickedValues = {}

game.Players.PlayerAdded:Connect(function(player)
	if not ClickedValues[player.Name] then
		ClickedValues[player.Name] = false
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	ClickedValues[player.Name] = nil
end)

re.OnServerEvent:Connect(function(player, isClickedValue)	

	ClickedValues[player.Name] = isClickedValue
	while ClickedValues[player.Name] == true do
		blinker1.Material = Enum.Material.Neon
		blinker2.Material = Enum.Material.Neon
		task.wait(0.5)
		blinker1.Material = Enum.Material.SmoothPlastic
		blinker2.Material = Enum.Material.SmoothPlastic
		task.wait(0.5)
		if ClickedValues[player.Name] == false or ClickedValues[player.Name] == nil then
			break
		end
	end
end)

Sorry, this is definitely not the most efficient/best way of fixing this, but I have to go soon but this will work.

The best way of doing it btw, if you do want to do it instead, (it’s not much more efficient) is by adding a bool value to the car, and detecting if it’s value is changed.

Now the problem is that the value is set to one player only (image), I’ll try testing it tomorrow in a normal game and not the studio test. But this is really weird. Any idea what’s going on?

The print is set up like this:

local rs = game:GetService("ReplicatedStorage")
local folder = rs:WaitForChild("RemoteEvents")
local re = folder.DoubleBlinker

local blinker1 = script.Parent.Parent.LBlinker
local blinker2 = script.Parent.Parent.RBlinker
local ClickedValues = {}

game.Players.PlayerAdded:Connect(function(player)
	if not ClickedValues[player.Name] then
		ClickedValues[player.Name] = false
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	ClickedValues[player.Name] = nil
end)

re.OnServerEvent:Connect(function(player, isClickedValue)	

	ClickedValues[player.Name] = isClickedValue
	print(ClickedValues[player.Name], player.Name)
	while ClickedValues[player.Name] == true do
		blinker1.Material = Enum.Material.Neon
		blinker2.Material = Enum.Material.Neon
		task.wait(0.5)
		blinker1.Material = Enum.Material.SmoothPlastic
		blinker2.Material = Enum.Material.SmoothPlastic
		task.wait(0.5)
		if ClickedValues[player.Name] == false or ClickedValues[player.Name] == nil then
			break
		end
	end
end)

image
If you it’s already night in your country then have a good night and thank you so much for the help!

I have some more time, let’s try the boolean value instead.

First add a Bool value into the car, name it like “Clicked”

Then this should 100% work unless my exhaustion made me mess up.

local blinker1 = script.Parent.Parent.LBlinker
local blinker2 = script.Parent.Parent.RBlinker
local Clicked = script.Parent.Parent.Clicked


re.OnServerEvent:Connect(function(player, isClickedValue)	
	Clicked.Value = isClickedValue
	while Clicked.Value == true do
		blinker1.Material = Enum.Material.Neon
		blinker2.Material = Enum.Material.Neon
		task.wait(0.5)
		blinker1.Material = Enum.Material.SmoothPlastic
		blinker2.Material = Enum.Material.SmoothPlastic
		task.wait(0.5)
	end
end)

Will try once I get to my computer (writing this from my phone :sweat_smile:). Once again thank you! :smile:

No worries, good luck on your project, God bless.

You lead me on the right path. This didn’t fully work but thanks to a few changes I was able to resolve the issue, I’m posting here only so there is a solution.
Script:

local rs = game:GetService("ReplicatedStorage")
local folder = rs:WaitForChild("RemoteEvents")
local re = folder.DoubleBlinker

re.OnServerEvent:Connect(function(player, isClickedValue)
	for i, v in pairs(game:GetService("Workspace"):GetChildren()) do
		if v:IsA("Model") and string.match(v.Name, player.UserId) then

			for m, n in pairs(v:GetChildren()) do
				if n:IsA("BoolValue") and n.Name == "Clicked" then
					local Clicked = n
					local blinker1 = n.Parent:WaitForChild("LBlinker")
					local blinker2 = n.Parent:WaitForChild("RBlinker")
					
					Clicked.Value = isClickedValue
					
					while Clicked.Value == true do
						blinker1.Material = Enum.Material.Neon
						blinker2.Material = Enum.Material.Neon
						task.wait(0.5)
						blinker1.Material = Enum.Material.SmoothPlastic
						blinker2.Material = Enum.Material.SmoothPlastic
						task.wait(0.5)
					end
				end
			end
		end
	end
end)

But thanks so much once again, God bless you. :smile:

1 Like

No worries, super glad I could help. God bless!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.