I was trying to send Bool value state from local to server but for some reason server side thinks that bool value is in my player instance
01 | --client |
02 | local BoolVal = player.Character.ExampleBool |
03 | BoolVal.Value = true |
04 |
05 | Event.OnClientEvent:Connect( function () |
06 | Event:FireServer(BoolVal) |
07 | end ) |
08 | --client |
09 |
10 |
11 | --server |
12 | ClickDetector.MouseClick:Connect( function (plr) |
13 | Event:FireClient(plr) |
14 | Event.OnServerEvent:Connect( function (BoolVal) |
15 | if BoolVal.Value = = true then |
16 | BoolVal = false |
17 | end |
18 | end ) |
19 | end |
20 | --server |
Error Message:Value is not a valid member of Player "Players.kubapro213748"
If you want to do Server-Client-Server Communication, it's highly recommended to use a RemoteFunction
instead. All you need to do is run Remote:InvokeClient()
in the Server and set RemoteEvent.OnClientInvoke
in the Client.
Warning: Remote:InvokeClient()
will throw an error if the Client throws an error, leaves, or disconnects, possibly breaking the entire script. To avoid that, it is HIGHLY recommended to wrap it in pcall()
since it catches any errors and returns a status boolean and the error message.
Server
01 | ClickDetector.MouseClick:Connect( function (plr) |
02 | local success, BoolVal = pcall (RemoteFunction.InvokeClient, RemoteFunction, plr) -- same as pcall(function() return RemoteFunction:InvokeClient(plr) end) |
03 | -- RemoteFunction:InvokeClient(plr) is the same as RemoteFunction.InvokeClient(RemoteFunction, plr) |
04 |
05 | if success then -- if RemoteFunction.InvokeClient was successfull (true) |
06 | -- BoolVal is still a BoolValue |
07 | BoolVal.Value = false -- you don't need to check if BoolVal.Value is true, if you set a false BoolValue's Value to false, it won't fire .Changed and :GetPropertyChangedSignal("Value") |
08 | else -- if fails/errors (false) |
09 | -- BoolVal is not a BoolValue anymore, rather a string that contains the error message |
10 | warn(BoolVal) -- warns the error message |
11 | end |
12 | end |
Client
1 | RemoteFunction.OnClientInvoke = function () -- this function will run if RemoteFunction.InvokeClient successfully reaches the Client |
2 | local character = player.Character |
3 | if not character or not character.Parent then -- if player's character hasn't loaded yet |
4 | character = player.CharacterAdded:Wait() -- waits until player's character fully loaded, then returns the player's character |
5 | end |
6 |
7 | return character:WaitForChild( "ExampleBool" ) -- sends the "ExampleBool" BoolValue to the server; Client must use WaitForChild when getting child of an instance since the Client loads first before the Server |
8 | end |
The problem here is on Line 14: Event.OnServerEvent:Connect(function(BoolVal)
.
The first Variable of the OnServerEvent
-Event is always the Player
. Though, there is also the issue that you're setting BoolVal
itself instead of its Value
to false.
Simply update your Server-Code to this:
1 | ClickDetector.MouseClick:Connect( function (plr) |
2 | Event:FireClient(plr) |
3 | Event.OnServerEvent:Connect( function (plr, BoolVal) |
4 | -- If you simply want to "invert" boolean (true to false and false to true), you should use `BoolVal.Value = not BoolVal.Value` instead. |
5 | if BoolVal.Value = = true then |
6 | BoolVal.Value = false |
7 | end |
8 | end ) |
9 | end |