Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I activate a multiplier when a certain item is bought?

Asked by 1 year ago

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?

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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.
0
You're a very nice person for helping anyway, but I don't recommend blindly giving answers when the person who asked hasn't really made an attempt to try it for themselves. You can give tips but we should help them with bugs and questions instead of writing the code for them ourselves. manith513 121 — 1y
0
ik, I just got bored and decided I would help someone. I made sure to tell what the code does so that way they know what each thing does rather than copying and pasting it without having any knowledge. Ill give tips next time. bittyboy1234 91 — 1y
Ad

Answer this question