Basic addition is not working?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Add cash to the player by 15
  2. What is the issue? Include screenshots / videos if possible!
    The sum is wrong. For example:
    The player’s cash is 15.
    The script adds 15, the sum should be 30 but it’s 45?
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked for solutions on the devforum, but they were dealing with decimals.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- Script #1
local Printer = {}
Printer.__index = Printer

function Printer.new(model)
	--Variables (Initialization and Declaration)
	local SoundService = game:GetService("SoundService")
	local NewPrinter = setmetatable({}, Printer)
	local PrintedEvent = Instance.new("BindableEvent")
	NewPrinter.Paper = 15
	NewPrinter.Security = 10
	NewPrinter.PrintAmount = 15
	NewPrinter.Printed = PrintedEvent.Event
	local Prompt = Instance.new("ProximityPrompt", model)
	Prompt.ObjectText = "Printer"
	Prompt.ActionText = "Print Money"
	
	--Behavior
	Prompt.Triggered:Connect(function(player)
		NewPrinter.Printed:Connect(function()
			local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
			if cash:IsA("IntValue") then
				cash.Value += NewPrinter.PrintAmount
				NewPrinter.Paper -= 1
				NewPrinter.Security -= 1
			end
		end)
		PrintedEvent:Fire(player)
	end)
	return NewPrinter
end
return Printer
--Script #2
local PrinterClass =  require(game:GetService("ReplicatedStorage"):WaitForChild("Modules"):WaitForChild("Classes"):WaitForChild("Printer"))
local Printer = PrinterClass.new(script.Parent)
Printer.Printed:Connect(function()
	print("Printed cash!")
	print("Printer Security: " .. Printer.Security)
	print("Printer Paper: " .. Printer.Paper)
	print("Printer Amount: " .. Printer.PrintAmount)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You have multiple connections as when you click a new connection is added and they stack, you only need one

-- Script #1
local Printer = {}
Printer.__index = Printer

function Printer.new(model)
	--Variables (Initialization and Declaration)
	local SoundService = game:GetService("SoundService")
	local NewPrinter = setmetatable({}, Printer)
	local PrintedEvent = Instance.new("BindableEvent")
	NewPrinter.Paper = 15
	NewPrinter.Security = 10
	NewPrinter.PrintAmount = 15
	NewPrinter.Printed = PrintedEvent.Event
	local Prompt = Instance.new("ProximityPrompt", model)
	Prompt.ObjectText = "Printer"
	Prompt.ActionText = "Print Money"
	
	--Behavior
	Prompt.Triggered:Connect(function(player)
		PrintedEvent:Fire(player)
	end)

		NewPrinter.Printed:Connect(function()
			local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
			if cash:IsA("IntValue") then
				cash.Value += NewPrinter.PrintAmount
				NewPrinter.Paper -= 1
				NewPrinter.Security -= 1
			end
		end)

	return NewPrinter
end
return Printer
--Script #2
local PrinterClass =  require(game:GetService("ReplicatedStorage"):WaitForChild("Modules"):WaitForChild("Classes"):WaitForChild("Printer"))
local Printer = PrinterClass.new(script.Parent)
Printer.Printed:Connect(function()
	print("Printed cash!")
	print("Printer Security: " .. Printer.Security)
	print("Printer Paper: " .. Printer.Paper)
	print("Printer Amount: " .. Printer.PrintAmount)
end)
1 Like
	Prompt.Triggered:Connect(function(player)
		PrintedEvent:Fire(player)
	end)

		NewPrinter.Printed:Connect(function()
			local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
			if cash:IsA("IntValue") then
				cash.Value += NewPrinter.PrintAmount
				NewPrinter.Paper -= 1
				NewPrinter.Security -= 1
			end
		end)

right here is what you mean?

I don’t see multiple connections.

Yeah, avoid nesting connections

Here is an example of what is happening which a brick color example:

These connections are being held in nil within the script environment and are hidden.

They still exist with the proof being you :Fire the signal once and multiple prints will be received or the function repeats multiple times.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.