Attempt to compare number and Instance script help?

Hi! I’m currently trying to make a shop for my game, but unfortunately I have everything finished but one thing, that is when you buy something you don’t receive it. You’d get coins in which you could then redeem them for gears. Every time I test it out on Studio, I always get the message: “ServerScriptService.BuyScript:5:attempt to compare number and Instance”. Here’s the scripts I’ll provide for this issue.

Script #1: BuyScript (troubling script)

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('BuyTool')

local function buyTool(player, tool)
	if player.leaderstats.Coins >= tool.Price.Value then
		
		player.leaderstats.Coins = tool.Price.Value
		
		local giveTool = ReplicatedStorage.ShopItems[tool.Name]:Clone()
		giveTool.Parent = player.Backpack
	
		local giveTool = ReplicatedStorage.ShopItems[tool.Name]:Clone()
		giveTool.Parent = player.StarterGear
	end 
end

remoteEvent.OnServerEvent:Connect(buyTool)

Script #2: LeaderboardScript

game.Players.PlayerAdded:Connect(function(plr)
	local f = Instance.new("Folder", plr)
	f.Name = "leaderstats"
	local coins = Instance.new("IntValue", f)
	coins.Name = "Coins"
	coins.Value = 0
	local function onPlayerJoin(player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
		coins.Parent = leaderstats
	end
	game.Players.PlayerAdded:Connect(onPlayerJoin)
end)

Script #3: GetCash (Coins)

waittime = 30 -- Time Between each hit
amnt = 10 --how much you get for it
function onTouched(part)
	local h = part.Parent:findFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:findFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:findFirstChild("leaderstats")
			if (stats~=nil) then
				local score = stats:findFirstChild("Coins")
				if (score~=nil) then
					score.Value = score.Value + amnt
				end
			end
		end
		
		script.Parent.Transparency = 1
		script.Disabled = true
		wait(waittime)
		script.Parent.Transparency = 0
		script.Disabled = false		


	end
end

script.Parent.Touched:connect(onTouched)
1 Like

Have you tried to changing this player.leaderstats.Coins to player.leaderstats.Coins.Value ?

1 Like

Yes I have, but if you were to buy something (ex. an item for 200 coins), your coin value wouldn’t drop down 200, but your coin counter would drop down all the way to the price of the item.

What @Infi_power said was right! But you where also because you also have to change the 4e line to make it remove the money and not set it to the price. The example below should work!

Example:

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('BuyTool')

local function buyTool(player, tool)
	if player.leaderstats.Coins.Value >= tool.Price.Value then
		
		player.leaderstats.Coins.Value -= tool.Price.Value
		
		local giveTool = ReplicatedStorage.ShopItems[tool.Name]:Clone()
		giveTool.Parent = player.Backpack
	
		local giveTool = ReplicatedStorage.ShopItems[tool.Name]:Clone()
		giveTool.Parent = player.StarterGear
	end 
end


remoteEvent.OnServerEvent:Connect(buyTool)

(If this helped. Mark it as the solution!)

4 Likes