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

How would I make this RemoteEvent toggle a boolean?

Asked by
Fatul 9
6 years ago
Edited 6 years ago

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.

0
Where is the script and where is the remote event? KingLoneCat 2642 — 6y
0
Local script is in StarterPlayerScripts, workspace.Events.Main is the remote event. Fatul 9 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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

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. Fatul 9 — 6y
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. KingLoneCat 2642 — 6y
Ad

Answer this question