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

How do I send Bool value state from local to server?

Asked by 2 years ago

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
02local BoolVal = player.Character.ExampleBool
03BoolVal.Value = true
04 
05Event.OnClientEvent:Connect(function()
06    Event:FireServer(BoolVal)
07end)
08--client
09 
10 
11--server
12ClickDetector.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)
19end
20--server

Error Message:Value is not a valid member of Player "Players.kubapro213748"

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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

01ClickDetector.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
12end

Client

1RemoteFunction.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
8end
0
Isn't this a way too overengineered solution to a simple issue? DindinYT37 246 — 2y
1
Thanks, now I know what remote functions are for kubapro213748 -13 — 2y
0
No it's not. Using a RemoteEvent IS OVERENGINEERING T3_MasterGamer 2189 — 2y
0
You're welcome! :D T3_MasterGamer 2189 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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:

1ClickDetector.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)
9end

Answer this question