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
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)