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 1 year 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

--client
local BoolVal = player.Character.ExampleBool
BoolVal.Value = true

Event.OnClientEvent:Connect(function()
    Event:FireServer(BoolVal)
end)
--client


--server
ClickDetector.MouseClick:Connect(function(plr)
    Event:FireClient(plr)
    Event.OnServerEvent:Connect(function(BoolVal)
        if BoolVal.Value == true then
            BoolVal = false
        end
    end)
end
--server

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

2 answers

Log in to vote
0
Answered by 1 year ago
Edited 1 year 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

ClickDetector.MouseClick:Connect(function(plr)
    local success, BoolVal = pcall(RemoteFunction.InvokeClient, RemoteFunction, plr) -- same as pcall(function() return RemoteFunction:InvokeClient(plr) end)
    -- RemoteFunction:InvokeClient(plr) is the same as RemoteFunction.InvokeClient(RemoteFunction, plr)

    if success then -- if RemoteFunction.InvokeClient was successfull (true)
        -- BoolVal is still a BoolValue
        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")
    else -- if fails/errors (false)
        -- BoolVal is not a BoolValue anymore, rather a string that contains the error message
        warn(BoolVal) -- warns the error message
    end
end

Client

RemoteFunction.OnClientInvoke = function() -- this function will run if RemoteFunction.InvokeClient successfully reaches the Client
    local character = player.Character
    if not character or not character.Parent then -- if player's character hasn't loaded yet
        character = player.CharacterAdded:Wait() -- waits until player's character fully loaded, then returns the player's character
    end

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

ClickDetector.MouseClick:Connect(function(plr)
    Event:FireClient(plr)
    Event.OnServerEvent:Connect(function(plr, BoolVal)
        -- If you simply want to "invert" boolean (true to false and false to true), you should use `BoolVal.Value = not BoolVal.Value` instead.
        if BoolVal.Value == true then
            BoolVal.Value = false
        end
    end)
end

Answer this question