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

How Can I Implement, Death Resulting In Loss Of Gold And Experience?

Ok so I am building a RPG game, and I am completely stumpted on how to create a script, where when you die you lose some Honor (Exp I can replace that out myself) depending upon the enemy that killed you & your Rank (Level). I also would like to do this with gold. Please help me out I WILL give credit, but if you were to use outside of my game I would like credit for the idea. Thank you, have a nice day.

2 answers

✓ Accepted answer

I'll explain the code in the script

game.Players.PlayerAdded:connect(function(Player) --When a player joins then do the following code...
	Player.CharacterAdded:wait() --Wait for the character to spawn
	wait(1) --Make sure everything loads
	Player.Character.Humanoid.Died:connect(function() --Once the character is loaded then find the humanoid and wait for it to die. Once it does, do the following code...
		if Player.leaderstats.Gold.Value >= 25 then --If the gold is bigger than or equal to 25 then do the following code...
			Player.leaderstats.Gold.Value = Player.leaderstats.Gold.Value-25 --Subtract 25 from the player's gold
		else --If the gold isn't bigger than or equal to 25 then do the following code...
			Player.leaderstats.Gold.Value = 0 --Make the player's gold 0.
		end --Close the If statement
	end) --Close the died event
end) --Close the PlayerAdded event

Enough detail for you?

3 comments

0

I think it should be, I appriciate the time you put into this.

0

Where would I put this? Workshop or starter GUI?

0

ServerScriptService would work fine.

Just so you know, this is very close to being against the rules of SH, especially since you didn't post any code to try and do this yourself.

The easiest way to do this is to call a function when the Player dies, giving it the Player and the Enemy that killed them, something like this:

function playerDied(Player, Enemy)
	if not Enemy then return end --Environmental deaths are 'free'
	local levelDifference = getLevel(Player) - getLevel(Enemy)
	if levelDifference <= -10 then return end --No loss for enemies greater than 10 levels above you.
	levelDifference = levelDifference + 10 --Convert this to a completely positive scale.

	setExperience(Player, getExperience(Player) - 10*levelDifference)
	giveGold(Player, -1*levelDifference)
end

4 comments

0

Would I have to post this within the Starter GUI and or enemy which give off the said loss?

0

This code will not work on its own. Take a closer look at the functions I am using. You will have to either expand the code there or write your own functions for that. As for placement, I would suggest placing this in the ServerScriptService and calling from there when a player is killed.

0

Okay I will try thank you very much. Not very good at this though, mind if I friend you and ask occasional questions?

0

The Died event might also be of some use, so I suggest you search for that in the wiki. It's an event of player.

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