Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

4 comments

0

Isn't this a way too overengineered solution to a simple issue?

1

Thanks, now I know what remote functions are for

0

No it's not. Using a RemoteEvent IS OVERENGINEERING

0

You're welcome! :D

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

This discussion is preserved as it was. New answers, comments, and votes are closed.