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

how do you make a currency conversion script?

i tried to make this script convert 500 "Money" into 1 "Credits"

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.leaderstats.Money.Value >= 500 then
			hit.leaderstats.Money.Value = hit.leaderstats.Money.Value - 500
			hit.leaderstats.Credits.Value = hit.leaderstats.Credits.Value + 1
		else
			print("Not enough money")
		end
	end
end)

basically i tried to make it find the humanoid then if they had 500 or more money it would give them 1 credit in return, but nothing happened

thanks

1 answer

✓ Accepted answer

hit would be the part that touched your script.Parent, you'd want to get the player, because that is where the leaderstats is stored.

The player service has a method to get the player from the character with internal validation. local Player = game.Players:GetPlayerFromCharacter(Character)

if a character is provided, it will return the player. If anything else is provided, it will return nil.

You can use it like this:

local PlayerService = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	--Get the player, returns nil if it doesnt exists, which is why it's in the next if statement.
	local Player = PlayerService:GetPlayerFromCharacter(hit.Parent)
    if Player and Player.leaderstats.Money.Value >= 500 then 
        Player.leaderstats.Money.Value = Player.leaderstats.Money.Value - 500
        Player.leaderstats.Credits.Value = Player.leaderstats.Credits.Value + 1
    else
        print("Not enough money")
	end
end)

4 comments

0

for some reason it prints not enough money when i have 1000 money?

0

Are you sure that you change your money value from the server and not the client? (For Filtering enabled)

0

no, how do you do that?

0

nvm it works now, i guess it didnt work before because i gave myself money. thanks!

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