Im having some scripting issue

so im making a simulator and im trying to sell a currency called “MnMs” (m&m simulator) and im trying to make a script where if i touch a part it will sell the currency and give me money which is called “Mcash”

the Sell script.

local Part = script.Parent
local id = 17679492
Part.Touched:Connect(function(hit)
	local H = hit.Parent:FindFirstChild("Humanoid")
	if H then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local leaderstats = player:WaitForChild("leaderstats")
			local Currency = leaderstats.Mcash -- change cash to your stat name
			local Selling = leaderstats.MnMs -- change apples to your stat name

			if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,id) then
				if Selling.Value > 0 then
					Currency.Value = Currency.Value + Selling.Value * 4
					Selling.Value = 0
				end
			else
				if Selling.Value > 0 then
					Currency.Value = Currency.Value + Selling.Value * 2
					Selling.Value = 0
				end
			end
		end
	end
end)

the leaderboard script:

local player = game:GetService("Players")

local function leaderboardsetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local mms = Instance.new("IntValue")
	mms.Name = "MnMs"
	mms.Value = 0
	mms.Parent = leaderstats
	
	local money = Instance.new("IntValue")
	money.Name = "Mcash"
	money.Value = 0
	money.Parent = leaderstats
end

game.Players.PlayerAdded:Connect(leaderboardsetup)
1 Like

What is the actual problem with the script? Any errors in output? Also I think you have to do local Currency = leaderstats:WaitForChild("Mcash") instead of just using . to define it.

there is no error but the money doesn’t update at all it just stay the same.
robloxapp-20210512-1048179.wmv (1.5 MB)

local Part = script.Parent
local id = 17679492

Part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local leaderstats = player.leaderstats
	    local Currency = leaderstats.Mcash -- change cash to your stat name
	    local Selling = leaderstats.MnMs -- change apples to your stat name

        if Selling.Value <= 0 then return end

		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,id) then
			Currency.Value += Selling.Value * 2
		else
			Currency.Value += Selling.Value
		end

        Selling.Value = 0
	end
end)

Try this in a normal script:

local Part = script.Parent
local id = 17679492
Part.Touched:Connect(function(hit)
	local H = hit.Parent:FindFirstChild("Humanoid")
	if H then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local leaderstats = player:WaitForChild("leaderstats")
			local Currency = leaderstats:WaitForChild("Mcash")-- change cash to your stat name
			local Selling = lleaderstats:WaitForChild("MnMs")-- change apples to your stat name

			if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,id) then
				if Selling.Value > 0 then
					Currency.Value = Currency.Value + Selling.Value * 4
					Selling.Value = 0
				end
			else
				if Selling.Value > 0 then
					Currency.Value = Currency.Value + Selling.Value * 2
					Selling.Value = 0
				end
			end
		end
	end
end)

The only thing that doesn’t work is the gamepass part. I might try fixing that later.

1 Like

this doesn’t work. Do you have any suggested video’s I can watch to learn this stuff better? Otherwise I think Roblox just hates me lol.

1 Like

This one doesn’t work sadly but if you have any suggested videos I can watch that would be good.

Maybe try this script?

-- When a player touches the parent object, their items will be sold for gold
local sellPart = script.Parent

-- Gives the player gold for each item they have
local function sellItems(playerItems, playerGold)
	-- Gives players 10 pieces of gold for each item
	local totalSell = (playerItems.Value * 10)
	playerGold.Value = playerGold.Value + totalSell
	playerItems.Value = 0
end

local function onTouch(partTouched)
	-- Looks for a Humanoid
	local character = partTouched.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	
	-- If a humanoid is found, gets leaderstats and calls sellItems function
	if humanoid then
		-- Get the player, so changes can be made to  the player's leaderstats
		local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		local playerStats = player:FindFirstChild("leaderstats")
		local playerItems = playerStats:FindFirstChild("MnMs")
		local playerGold = playerStats:FindFirstChild("Mcash")
		-- Sells Items and then changes the player's spaces and money
		sellItems(playerItems, playerGold)
	end
end

sellPart.Touched:Connect(onTouch)

This is just a basic sell script though.

1 Like

nope it don’t work either but its fine cause ill try and figure it out but otherwise imma just give up and try creating a simpler game like a tycoon.

That’s odd considering it worked for me. It’s in a normal script inside of the part correct?