Setting player's boolean value to true using a local script

Hello so basically im trying to make a zone system in which u can buy the first zone and by buying it, a boolean’s value is set to true, the problem is that it doesn’t work for some reason:

local script inside a proximity prompt:

local plr = game.Players.LocalPlayer
	local leadestats = plr:WaitForChildChild("leaderstats")
	local goldStats = leadestats and leadestats:WaitForChild("Cash")
script.Parent.Triggered:Connect(function()
	plr:FindFirstChild("test").BoolValue.Value = true
	end)

the boolean:

image

1 Like

You’re going to have to do it on the server to set the BoolValue to true, use remote events here’s an example.

Server:


local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent -- # Change this to wherever you store your remotes/current remotes

RemoteEvent.OnServerEvent:Connect(function(player)
    local cashRequired = 0 -- # Change this to cash required
    if player:WaitForChild("leaderstats"):WaitaForChild("Cash").Value >= cashRequired then
       player:WaitForChild("test"):WaitForChild("BoolValue").Value = true
       player:WaitForChild("leaderstats"):WaitForChild("Cash").Value -= cashRequired
    end
end)

Client:


script.Parent.TriggerEnded:Connect(function()
  game.ReplicatedStorage.RemoteEvent:FireServer() -- # set this to the remote that's connected to the event
end)

2 Likes

It sadly doesn’t work. Doesn’t take away the Cash or set the value to true.

You need to do this on server-sided script, not local script.

i understand but im also trying to delete the zone so the player can walk through, but if its on the server-side anyone will be able to walk through without paying.

1 Like

I see two issues:

  1. A typo: local leadestats = plr:WaitForChildChild("leaderstats"), it should be WaitForChild not WaitForChildChild.
  2. A replication issue: When a local script makes changes, those changes won’t replicate to the server(except for very specific scenarios like character physics). This means that if you change a value(like a BoolValue) on the client, the server will see the old value, not the new one. Instead, you need to switch to a server script or use Remote Events to directly communicate with the server, from the client.

Okay i figured everything by myself. thanks for help, i also need help with saving the boolean using datastores but thats something for another topic, ima post the link to that in a second

If serverSide sets the value to true, then you can just check on client if the value has changed.

--StarterCharacter script
local localCharacter = script.Parent

local booleanFolder = localCharacter:WaitForChild("test")
local accessBoolean = booleanFolder["BoolValue"]

local function onValueChanged()
    local booleanValue = accessBoolean.Value

    if booleanValue then
        -- //Do something
    end
end

accessBoolean["Changed"]:Connect(onValueChanged)

Here is simple script which could be put in starterCharacter and should fix this issue.