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

Check for boolvalue to unlock a door not working?

Asked by 10 months ago

My code is a followup to a basic admin command I made. Chefs can use a !unlock command with the argument being user, and it will remove a forcefield for the player. What I have done so far is making a script that gives each player a boolvalue titled "forcefield" when they join that is automatically set to true. The unlock command sets the target user's forcefield to false. What I want is when a player touches the door, it checks if their boolvalue is FALSE, and if it is false it lets the player through by turning off cancollide. It is not working though. (The script is inside of the door)

local door = script.Parent

local function dothis(otherPart)
    if otherPart.Parent.forcefield.Value == false then
        script.Parent.CanCollide = false
        print("no permission")


Keep in mind I did check the command's code and the "on join script"'s code and they both working fine. The only hang up is the doors script.

1 answer

Log in to vote
Answered by 10 months ago
Edited 10 months ago

Is you're command script a Client/Local script? If so, use RemoteEvents to make the BoolValue go false in the Server. This is because the BoolValue is only being changed to the client (player's device) and not to all players and the server. I also recommend making your door script in the Client.


-- in your !unlock command
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") -- this won't work unless you created a RemoteEvent inside ReplicatedStorage



-- inside ServerScriptService
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

local OnServerEvent = function(player, bool) -- the function will run if the client script used RemoteEvent:FireServer()
    local character = player.Character or player.CharacterAdded:Wait() -- created a variable of the player's character
    local forcefield = character:FindFirstChild("forcefield") -- the forcefield BoolValue

    if forcefield ~= nil then -- checks if player has the forcefield BoolValue
        forcefield.Value = bool
    else -- if no forcefield
        local newValue ="BoolValue", character)
        newValue.Name = "forcefield"
        newValue.Value = true

        OnServerEvent(player, bool) -- retries


Note: This answer only applies if your command script is a Client script.

(Client scripts can only be used inside StarterPlayerScripts, StarterCharacterScripts (Character), StarterGui, StarterPack (Tools), and ReplicatedFirst. They are also known as Local Scripts. Their old icon has a person's face and the new icon has a computer monitor in it.) Learn more: Documentation Fandom Wiki


Answer this question