Need some scripting math help

Your script had some issues, besides the one Yaroslav mentioned. Here they’re fixed.

local rebirthBaseCost = game.ReplicatedStorage.RebirthValue.Value --Any changes to value will also affect the BillboardGui.Text
local newRebirthCost = rebirthBaseCost * ((rebirthStat.Value * 2.5) * 1.05)
		
if rebirthStat.Value == 0 then -- If we have not rebirthed before
	if coinStat.Value < rebirthBaseCost then return end -- If our current coins is lower than the base rebirth cost, return end. Return in this case just means that the script wont go further.
else -- If not first time rebirthing
	if coinStat.Value < newRebirthCost then return end -- If our current coins is lower than the newRebirthCost then return end. Return in this case just means that the script wont go further.
end
-- If the script did not "return" we know we had enough coins, so just give them rebirth stuff.
coinStat.Value = 0
rebirthStat.Value += 1
rebirthToken.Value += 1 -- This is a value the player can use to increase their stats
script.RebirthSound:Play()

I tried your solution and nothing happened so I added a couple print statements to see if it activates when touched and nothing happened. I am unsure what the problem is.

I’ve added notes, it should work as intented. Did you put it correctly into the function?

I think so, here’s the whole thing.

-- Rebirth Upgrade Script --

local Players = game.Players
local rebirthPad = script.Parent
local db = true

local function onPartTouched(otherPart)
	local parent = otherPart.Parent
	local humanoid = parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and db then
		db = false
		
		local player = Players:GetPlayerFromCharacter(parent)
		local leaderstats = player.leaderstats
		local playerstats = player.playerstats
		local rebirthStat = leaderstats and leaderstats:FindFirstChild("Rebirth")
		local coinStat = leaderstats and leaderstats:FindFirstChild("Coins")
		local rebirthToken = playerstats and playerstats:FindFirstChild("RebirthToken")
		
------------- Put code in here --------------------------------------------------		
		
		local rebirthBaseCost = game.ReplicatedStorage.RebirthValue.Value --Any changes to value will also affect the BillboardGui.Text
		local newRebirthCost = rebirthBaseCost * ((rebirthStat.Value * 2.5) * 1.05)

		if rebirthStat.Value == 0 then
			if coinStat.Value < rebirthBaseCost then return end
			print(rebirthBaseCost)
		else
			if coinStat.Value < newRebirthCost then return end
			print(newRebirthCost)
		end
		
		coinStat.Value = 0
		rebirthStat.Value += 1
		rebirthToken.Value += 1 -- This is a value the player can use to increase their stats
		script.RebirthSound:Play()
		
		task.wait(1)
		db = true
	end	
end

rebirthPad.Touched:Connect(onPartTouched)

--[[

rebirth token "rToken" would be used to increase,
1+ gold
1+ run speed
1+ luck
+1 rebirth coin - used in shop or something

--]]

Your prints wont print, if coinStat.Value is below the cost.
image

Edit:

Also here I fixed your variables

		local player = Players:GetPlayerFromCharacter(parent)
		if not player return end
		local leaderstats = player:FindFirstChild("leaderstats")
		local playerstats = player:FindFirstChild("playerstats")
		if not leaderstats or not playerstats return end
		local rebirthStat = leaderstats:FindFirstChild("Rebirth")
		local coinStat = leaderstats:FindFirstChild("Coins")
		local rebirthToken = playerstats:FindFirstChild("RebirthToken")
		if not rebirthStat or not coinStat or not rebirthToken return end

That’s true but this is also true.

it doesn’t activate on touch.

Try insert the updated variables.

Edit: Edited the variables once more.

Oh I didn’t see that. hold on!

Could you paste all the changes together and my copy/paste is making all the returns have red underlines and I’m unsure where the issues lie.

So I added then’s and the red lines went away. The first rebirth worked as intended but there was no second. Something’s causing it seeing as I even gave myself 50k coins but it still didn’t rebirth.

If that does not work, just stick to your original plan.

local Players = game.Players
local rebirthPad = script.Parent
local rebirthBaseCost = game.ReplicatedStorage.RebirthValue.Value --Any changes to value will also affect the BillboardGui.Text
local db = {}

local function setdb(player,Status)
	db[player.name] = Status
end

local function onPartTouched(otherPart)
	local parent = otherPart.Parent
	local player = Players:GetPlayerFromCharacter(parent)
	if not player return end
	if db[player.name] return end
	setdb(player,true)

    local player = Players:GetPlayerFromCharacter(parent)
    if not player then setdb(player) return end
    local leaderstats = player:FindFirstChild(leaderstats)
    local playerstats = player:FindFirstChild(playerstats)
    if not leaderstats or not playerstats then setdb(player) return end
    local rebirthStat = leaderstats:FindFirstChild(Rebirth)
    local coinStat = leaderstats:FindFirstChild(Coins)
    local rebirthToken = playerstats:FindFirstChild(RebirthToken)
    if not rebirthStat or not coinStat or not rebirthToken then setdb(player) return end


	if rebirthStat.Value == 0 then
		print("Have not rebirthed before.The player has enough coins:",coinStat.Value>=rebirthBaseCost)
		if coinStat.Value < rebirthBaseCost then setdb(player) return end
		print(rebirthBaseCost)
	else
		local newRebirthCost = rebirthBaseCost * ((rebirthStat.Value * 2.5) * 1.05)
		print("Have rebirthed before.The player has enough coins:",coinStat.Value>=newRebirthCost)
		if coinStat.Value < newRebirthCost then setdb(player) return end
	end
	
	coinStat.Value = 0
	rebirthStat.Value += 1
	rebirthToken.Value += 1 -- This is a value the player can use to increase their stats
	script.RebirthSound:Play()
	
	task.wait(1)
	setdb(player)
end

rebirthPad.Touched:Connect(onPartTouched)
1 Like

Very robust, awesome work tyvm. I got this error and I have no experience with

if not player then setdb(player) return end

I’m sure you copied it before I edited, try again. There was a | symbol at line 19 by mistake.

Works perfectly tyvm. I test in and out the game too fast I think as the Datastore values don’t always save but at least the rebirth system works now. Just have to tell the BillboardGui to display the coins needed to rebirth now and it’s good to move on to the next part. I can’t thank you enough.

For DataStore I would suggest DataStore2. It’s almost plug&play for beginners.

I was thinking that too but it took me months just to understand what Datastore does and i’m still struggling with it tbh. Datastore2 scares me as I’m on a time crunch now.

How could i make a sort of purchase saving system? - #6 by TortenSkjold Here is almost an plug and play understanding of how to set it up.

Extended info:

I would recommend that you use DataStore2. It’s a very good module for those who wish to just have a datasave system that works without much hassle!
Here’s the setup that I have for my own games. I’ve simplified it for you!

Have the module downloaded and place it into ServerStorage. Also create a “StringValue” in ServerStorage, that we use to change our DataVersion with, this way we can “wipe” all data in the future, just by changing the value of the DataVersion StringValue in ServerStorage. (I personally write “v1” as the default version. Also create a BoolValue in ServerStorage called “SaveInStudio”, if this boolvalue is false, then it won’t save data while you’re testing in studio.

I will definitely look into it again but I have noone around me that knows this stuff so I’m on my own. Maybe I could add you as a resource if you’re not too busy for the odd question here and there if you don’t mind?

You can always PM me here on the forum.

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