I've been starting work on my first tycoon recently, and it's the first time I'm trying Roblox studio and Lua. I wanted to make it so that whenever a specific item is bought it gives an extra amount of money every 5 seconds. It's currently made so that you get 5 cash every 5 seconds, and I'm trying to make it so that when you buy a specific item from a button, that amount doubles, and you get 10 cash every 5 seconds. Can anyone help me?
Inside the button, add this script. Server Script
players = game:GetService("Players") -- Gets the service called Players which is where the player is stored script.Parent.Touched:Connect(function(hit) -- This function will fire when something has touched the button. if hit:FindFirstChild("Humanoid") then -- Checks that what's touching it is a player local plr = players:GetPlayerFromCharacter(hit.Parent) -- This will make the player service find the player based on the Name of the character that is touching the part plr.Multiplier.Value = plr.Multiplier.Value + plr..Multiplier.Value -- Changes the multiplier by 1 so we can double the amount of money the player gets. script.Parent:Destroy() -- Destroys the button so the player cant buy it again.
Next, you want to make a script to give the player leaderstats if you haven't done so already. In ServerScriptService, Make a Server Script and add a RemoteEvent inside of ServerScriptService.
game.Players.PlayerAdded:Connect(function(plr) -- Fires when a player is added to the game. local Leaderstats = Instance.new("Folder") -- Creates a folder Leaderstats.Name = Leaderstats -- Changes the name of the folder to Leaderstats the name of the folder has to be Leaderstats. leaderstats.Parent = plr -- Puts Leaderstats inside the player. local money = Instance.new("NumberValue") -- From here down, it does the same thing, except creates number values which can be stored inside of leaderstats to represent cash and other things. Money.Name = money Money.Parent = Leaderstats Money.Value = 0 local Multiplier = Instance.new("NumberValue") Multiplier.Name = Multiplier Multiplier.Parent = plr Multiplier.Value = 1
After you have done this, you want to make a local script and put it in starterGui.
while wait(5) do -- fires every 5 seconds game:GetService("ServerScriptService").RemoteEvent:FireServer() -- fires a RemoteEvent for the server. end
after you have done this, you want to add another Server Script into ServerScriptService
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr) -- Fires when the RemoteEvent is fired. plr:WaitForChild("Leaderstats").Money.Value = plr:WaitForChild("Leaderstats").Money.Value + 5 * plr.Multiplier -- Changes the money by 5 * the multiplier.