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

How would I make this RemoteEvent toggle a boolean?

Server

script.Parent.OnServerEvent:connect(function(player, currentKey)
	local cultivating = false
	local toggle = false
	
	if currentKey == "Z" then
		if cultivating == false then
			cultivating = true
		else
			cultivating = false
		end
	else return end
		
	print(cultivating)
		
end)

Local

local UIS = game:GetService('UserInputService')

UIS.InputBegan:connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		local currentKey = input.KeyCode.Name
		workspace.Events.Main:FireServer(currentKey)
	end
end)

It always returns true. Never goes true, then false, then true, etc.

I've messed with a lot of variations so far and it appears to me any type of FE event just cant handle toggles all that well.

2 comments

0

Where is the script and where is the remote event?

0

Local script is in StarterPlayerScripts, workspace.Events.Main is the remote event.

1 answer

✓ Accepted answer

Hey Fatul,

Your issue is very easy to solve and it's a very small issue. You declared the variable cultivating inside of the function so, every time the function is called, it declares cultivating as false and then it just makes it true every time. The solution to this is simple, just declare cultivating outside the function.

local event = script.Parent;
local cultivating = false;

event.OnServerEvent:Connect(function(player, key)
	if key == "Z" then
		if cultivating == false then
			cultivating = true;
		else
			cultivating = false;
		end
	end

	print(cultivating)
end)

##Well, I hope I helped with this simple issue and have a nice day/night.

~~ KingLoneCat

2 comments

0

I've tried this before and i'm not sure why, but everyone in the server is using the same 'cultivating' variable. Thus, if one player is cultivating, and another player enables it, it'll cancel out both players variable to false.

0

Then use a local script to do the toggling. I don't understand why you are even doing it in a server script and I'm not sure as to why you are even checking for the KeyCode in the Server script. Just handle all that in the local script.

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