So I'm trying to make a Roblox game at 1 am because who doesn't and i ran into a small problem: I have a variable that wont update in my server script even if i can see it being updated in the explorer and leaderstats.
My cultivation toggle script (local script under the text button):
local button = script.Parent
local cultivating = game.Players.LocalPlayer.leaderstats.Cultivating
button.MouseButton1Click:Connect(function()
if cultivating.Value == false then
cultivating.Value = true
button.Text = "Stop Cultivating"
else
cultivating.Value = false
button.Text = "Start Cultivating"
end
end)
My leaderstats script (server script under serverscriptservice):
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local qi = Instance.new("IntValue")
qi.Name = "Qi"
qi.Value = 0
qi.Parent = leaderstats
local cultivating = Instance.new("BoolValue")
cultivating.Name = "Cultivating"
cultivating.Value = true
cultivating.Parent = leaderstats
end)
My cultivation script (server script under serverscriptservice):
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local qi = leaderstats:WaitForChild("Qi")
local cultivating = leaderstats:WaitForChild("Cultivating")
while true do
if cultivating.Value == true then
qi.Value += 1
end
wait(0.5)
end
end)
Basically the variable "cultivating" is getting updated by the local script which works but doesn't get updated inside the cultivation script.
I was expecting this to give me 1 qi every 0.5 seconds if I'm cultivating.
I tried using AI to help me (time wasted), tried debugging with a bunch of print statements and got to the conclusion that it is indeed the variable not updating inside the script. I have even tried declaring the variable inside the loop without any success.
In your cultivation toggle script, are you changing the cultivating
value on the client and not on the server. That's why your cultivation script which is server-sided is not updating because the cultivating
never updates on the server but on the client so the server-side script can't detect it.
Roblox uses a client-server model so to communicate on the server it is necessary to use a RemoteEvent
to tell the server that the user is cultivating and then use that information to add qi
.
The RemoteEvent object facilitates asynchronous, one-way communication across the client-server boundary without yielding for a response. This communication can be directed from one client to the server, from the server to a specific client, or from the server to all clients.
— https://create.roblox.com/docs/reference/engine/classes/RemoteEvent
In this case, you would use the Client → Server remote event to tell the server that a user is cultivating.
The changes you should make in your script would include: