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

When you touch a brick, it changes your level if you have enough 'Cash', how to?

So I've been making a Zombie Shooter FPS game, and been trying to get a level system to function to no avail due to my scripts not focusing on a level system but rather focusing on in-game currency drops for better weapons(I know error on my part), but I've been doing some thinking and though "What if a player touches a brick, it takes a certain amount of currency from them, and levels them up accordingly".

Sadly, I have no the sweetest clue as to how to go about coding this, and it may sound dumb on my end, but I do not know. I've done some research and found a code I'll put below. My currency is "DiamondDollors" and I was thinking that an increase of "100" DiamondDollors for every level would be suffice.

Example; Level 1 - 0 DiamondDollors Level 2: 100 DiamondDollors Level 3: 200 DiamondDollors Level 4: 300 DiamondDollors etc...

Here's some code I found to change the stat, but I wasn't planning on using it, it's the closest idea to what I'm trying to do if it helps any of you maybe better understand!

local part = script.Parent -- Variable for the part which the character is going to touch.

part.Touched:connect(function(hit) -- An anonymous function(Hope you know how to make one)
    local hum = hit.Parent:WaitForChild("Humanoid") -- Variable to confirm it's a humanoid that touches that part.
    if hum then -- If statement(Hopefully you know how to make one)
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- Variable for player using :GetPlayerFromCharacter() method.
        plr.leaderstats.GameMode.Value = "Normal" -- Changes the value of the StringValue
    end
end)

Thank you all, any help is appreciated!

1 comment

0

Sort of. Like a level system, but instead of using XP, it uses cash. And it only occurs when a player touches a brick in the map instead of auto occuring. And that brick takes the correct amount of DiamondDollors from the players account if they have it and changes their level accordingly.

1 answer

✓ Accepted answer

ServerScript

local part = script.Parent
local debounce = true

part.Touched:Connect(function(hit)
if debounce == false then return end
debounce = false
    if hit.Parent:FindFirstChild("Humanoid") then
        local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		local stats = plr:WaitForChild("leaderstats") -- Change this to the Folder or Model containing the stat values
		local dollars = stats:WaitForChild("DiamondDollors")
		local level = stats:WaitForChild("Level")
		local subtract = math.abs((level.Value * 100) - 100)
		
		if dollars.Value >= subtract then
			dollars.Value = dollars.Value - subtract
			level.Value = level.Value + 1
		end
    end
debounce = true
end)

I'll use your example script as a template The debounce is to prevent the event from firing multiple times.

The math function is based on what you gave in your question, so that every level takes an additional 100 diamond dollars from the last time they paid.

1 comment

0

Awesome, thank you so much!

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