Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how do you make a currency conversion script?

Asked by 5 years ago

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

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
5 years ago

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)
0
for some reason it prints not enough money when i have 1000 money? poopypigeon245 22 — 5y
0
Are you sure that you change your money value from the server and not the client? (For Filtering enabled) RubenKan 3615 — 5y
0
no, how do you do that? poopypigeon245 22 — 5y
0
nvm it works now, i guess it didnt work before because i gave myself money. thanks! poopypigeon245 22 — 5y
Ad

Answer this question