I have a problem with my chests system

So I’m trying to make text change for my chests from “Collect!” to “Can’t Collect!” and vice versa, and it will be different for the player, but LocalScripts do not run in the Workspace.

So when you collect the chest, it will change to “Can’t collect!”, and when the chest is ready again, it’ll change back to “Collect!”

How can I fix this problem?

Try this:

local text1 = "Collect"
local text2 = "Can't collect"

--Be sure to define "textlabel"

if textlabel.Text == text1 then
    textlabel.Text = text2
elseif textlabel.Text == text2 then
    textlabel.Text = text1
end
1 Like

To @GodpraiserNOW,

The script you put will change for the server, but I want it to just change for the player who collected.

If I wanted it to change for the whole server, I wouldn’t have come here, since that is one of the easiest things to do… :expressionless:


To @OIogist, a touched event :smiley:

Do you open the chests from a mouse click or a touched event?

Oooohhhh I get it

You would need a remote event. Carry textlabel to the localscript using arguments/parameters.

@GodpraiserNOW that would literally do nothing as the server will change it for all players, not the client! :expressionless:

Wait I read that wrong…

I’ll try it out!

Leave the text blank on the server then or don’t change the text in a serverscript once the game has started

Ok so, try this:

This will be the sever script:

local chest = --define chest here
local remote = --define remote event here

chest.Touched:Connect(function(hit)
    local player = game.Players:FindFirstChild(hit.Parent.Name)
    remote:FireClient(player)
end)

And in a local script, type the following:

local remote = --define remote event here
local textlabel = --define textlabel here

local opened = "Collect"
local closed = "Can't Collect"


remote.OnClientEvent:Connect(function()
    if textlabel.Text == opened then
        textlebel.Text = closed
        --give the player whatever they want here (if it is a tool, you may want to fire another remote event to the server so it shows up for everyone else in the server)
        wait(20) --Cooldown time until chest is collectable again
        textlabel.Text = opened
    end
end)
1 Like

Oh also, for the textlabel, you may want to get it from the player’s PlayerGui, as opposed to the StarterGui

local player = game.Players.LocalPlayer
local textlabel = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("TextLabel")
--define it properly as how you named your textlabel and its parents

Uhh… There could be a slight problem.

I use DataStores for this (So basically it’s a daily chest) and I need to find another way to change the text back to open…

And I am not using ScreenGuis, but BillboardGuis, and if I were doing ScreenGuis, I would not have come here!

Ohh, i see (I’m assuming you just use a boolvalue for if you opened it or not) Do this instead:

ServerScript:

local chest = --define chest here
local remote = --define remote event here

chest.Touched:Connect(function(hit)
    local player = game.Players:FindFirstChild(hit.Parent.Name)
    local daily = player:FindFirstChild("Opened") --define this properly (i would recommend making this a BoolValue)

    if daily.Value == false then
        daily.Value = true
        remote:FireClient(player)
        --reward player here
    end
end)

LocalScript:

local remote = --define remote event here
local textlabel = --define textlabel here

local opened = "Collect"
local closed = "Can't Collect"


remote.OnClientEvent:Connect(function()
    if textlabel.Text == opened then
        textlebel.Text = closed
    end
end)
1 Like

Don’t use DataStore on a localscript. I’m pretty sure it won’t work. If you have to, use remote events (client to server) to save data (unless if ur already on a serverscript)

1 Like

Since you are changing the BoolValue to true from the serverscript and changing the textlabel from a local script, the server recognizes that the player has obtained the reward and fires a message to the client, which changes their textlabel

I never said I was using DataStores on a localscript. :expressionless:

So you’re using SurfaceGui’s? if so, then put a localscript in StarterPlayer (so the localscript goes in the player object). make a touched event and fire a remove event to the localscript and change the text on the localscript. Be sure to carry the textlabel over to the localscipt.

--SERVERSCRIPT
local chest = script.Parent? --make sure that this is a part and not a folder/model
local textlabel = chest:WaitForChild('SurfaceGui'):WaitForChild('TextLabel') --redefine this to the textlabel
local remoteEvent = --define remove event here

chest.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        remoteEvent:FireClient(plr,textlabel)
    end
end)

--LOCALSCRIPT
local remoteEvent = --define remote event here

local text1 = "Collect"
local text2 = "Can't collect"

remoteEvent.OnClientEvent:Connect(function(textlabel)
    if textlabel.Text == text1 then
        textlabel.Text = text2
    elseif textlabel.Text == text2 then
        textlabel.Text = text1
    end
end)

No, not SurfaceGuis!!!

Fine, I’m just gonna give you pictures:

1 Like

So just change “SurfaceGui” in my script to “BillboardGui.” Also change whatever else you need to.

First of all this is not what I’m looking for. That’s just changing the text, but I need it to change back when the chest is available again.

Second, the code has absolutely no debounce so the text will keep changing in a matter of milliseconds.

To make it change back, whenever your cooldown is up, you can just set’s the player’s Opened value back to false and fire a remote event to that client that changes the textlabel back to “Collect”

Also, in my script, there is a “local” debounce

But then this goes back to what I said oh, 10 replies ago.

I’m using DataStore so it will cooldown while the player is gone, and let me ask you, “How will you change the text if there is no player?”