Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

works in studio but not in game Is there any fix to this? Ive been trying to fix it for a long time.

Local script inside GUI button:

local player = game.Players.LocalPlayer
local mm = require(script.ModuleScript)
local has = script.Has -- Bool Value
has.Value = false
script.Parent.MouseButton1Down:connect(function()
	if player.leaderstats.Cash.Value >= script.ModuleScript.Mta.Value then
		local Mta = script.ModuleScript.Mta.Value -- How much it cost
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - Mta
		Mta = Mta * 2 
		script.Parent.Text = 'Hire Bil for $'..Mta
		has.Value = true
		
	end
end)

Script inside LocalScript:

local mm = require(script.Parent.ModuleScript)

repeat wait() until script.Parent.Has.Value == true
mm.Bil()

Module script inside LocalScript:

local Cash = {}

function Cash.Bil()
	while wait(.5) do
		local player = game.Players.LocalPlayer
		local mtg = script.Mtg -- how much it gives you per second
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + mtg.Value	
	end
end

return Cash

mtg is a IntValue. Any fix for this?

2 comments

0

This is an extremely strange way to organize this code

0

It is not a good idea to manage the players leaderstats using a local script.

1 answer

If you're using FilteringEnabled (you should be), a LocalScript's change to the leaderstats won't be visible to other players. So really, this should involve a Script object. I will neglect that at first.

Regular Script objects don't run in PlayerGui.

Simple Local Solution

You are organizing your code strangely. It can be reduce to this:

local player = game.Players.LocalPlayer

-- Load the amount of money the player has from their leaderstats
local money = player:WaitForChild("leaderstats"):WaitForChild("Cash").Value

-- A function to modify the amount of money player has
function earn(amount)
	money = money + amount
	player.leaderstats.Cash.Value = money
end

-- Represents an upgrade that earns you money
local worker = {rate = 0, cost = 10}

-- What to do on buy button
script.Parent.MouseButton1Down:connect(function()
	if money >= worker.cost then
		earn(-worker.cost)
		worker.rate = worker.rate + 1
	end
end)

-- In the background, earn money from the worker
while wait(0.5) do
	earn(worker.rate)
end

With Remotes

-- ServerScript

local workerByPlayer = {}

function earn(player, amount)
	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	cash.Value = cash.Value + amount
end

-- A helper that attempts to spend money, returning where it was successful.
function spend(player, amount)
	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	if cash.Value >= amount then
		cash.Value = cash.Value - amount
		return true
	end
	return false
end

function newPlayer(player)
	Instance.new("IntValue", player).Name = "leaderstats"
	Instance.new("IntValue", player.leaderstats).Name = "Cash"
	workerByPlayer = {rate = 0, cost = 10}
end

-- listen to a RemoteFunction for "buy" clicks
function game.ReplicatedStorage.Upgrade.OnServerInvoke(player)
	if spend(player, workerByPlayer[player].cost) then
		-- spend money and do an upgrade
		workerByPlayer[player].cost = workerByPlayer[player].cost * 2
		workerByPlayer[player].rate = workerByPlayer[player].rate + 1
		return true
	end
	return false
end

while wait(0.5) do
	-- give each player their money
	for player, worker in pairs(workerByPlayer) do
		earn(player, worker.rate)
	end
end
-- LocalScript
local debounce = false
script.Parent.Button1Down:connect(function()
	if debounce then
		return
	end
	debounce = true
	game.ReplicatedStorage.Upgrade:InvokeServer()
	debounce = false
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.