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

My shop script is not working? And no i don't want a link i want a script

For a brief background i'm creating a script that when you touch it you get an item with a specific amount of points but it's not working

Either it's not working, or it's giving me infinite of the same items. I dont want any debouce or anything

Is there anyway to check if the player already has the item?

local Part = game.Workspace.BuyPart




local RS = game:GetService("ReplicatedStorage")
local Sword = RS.Tools:FindFirstChild("SpeedCoil")
local Players = game:GetService("Players")

Part.Touched:Connect(function(hit)
	
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if Player.leaderstats.Points.Value >= 500 then
			
		local SwordClone = Sword:Clone()
		if Player.Backpack:WaitForChild("SpeedCoil") then
		SwordClone.Parent = Player.Backpack
		SwordClone = false
	end
	
	local GravityCoil = RS.Tools:FindFirstChild("GravityCoil")
	local Part1 = game.Workspace.BuyPart1
	
	Part1.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			
			
			if Player.leaderstats.Points.Value >= 7000 then
					
				local CoilClone = GravityCoil:Clone()
			if Player.Backpack:WaitForChild("GravityCoil") then
				CoilClone.Parent = Player.Backpack
				CoilClone = false
				
	end
	
	local Part2 = game.Workspace.BuyPart2
	local Tool = RS.Tools:FindFirstChild("PointGiverTool")
	
	Part2.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then
	
			
			if Player.leaderstats.Points.Value >= 10000 then
				
				local ToolClone = Tool:Clone()
				
				if Player.Backpack:WaitForChild("PointGiverTool") then

				ToolClone.Parent = Player.Backpack
				ToolClone = false
				
			end
			end
			end
			end)
				end
				end
				end)
					end
					end)

Thank you for your time

4 comments

0

check also if the player's character has the tool, cuz if the player equips the tool it will delete the tool in the player's backpack and clone it in the player's character

0

can you give me the script

0

Lol, you need a debounce

0

JUST GIVE ME A SCRIPT I HAVE USED 3 DAYS ON THIS GARBAGE

1 answer

✓ Accepted answer

Firstly, you're being repetitive within your code. This is an extremely poor practice and can cause organizational problems which lead to issues integrating conditions properly. Please use functions to handle information processing if you require to perform multiple homogeneous actions.

To debounce the purchase, you can look for the existing item within the Backpack and call return to close the function, otherwise, proceed with duplicating the item.

local replicatedStorage = game:GetService("ReplicatedStorage")
local tools = replicatedStorage:WaitForChild("Tools")

local speedCoil = tools:WaitForChild("SpeedCoil")
local gravityCoil = tools:WaitForChild("gravityCoil")
local pointGiverTool = tools:WaitForChild("pointGiverTool")

local players = game:GetService("Players")

local buyPart0 = workspace.BuyPart
local buyPart1 = workspace.BuyPart1
local buyPart2 = workspace.BuyPart2

local function handlePlayerPurchase(player, cost, item)
	local humanoid = Player.Parent:FindFirstChildOfClass("Humanoid")
	if (humanoid) then
		player = players:GetPlayerFromCharacter(player.Parent)
		local backpack = player:WaitForChild("Backpack")
		local leaderstats = player:WaitForChild("leaderstats")
		local points = leaderstats:FindFirstChild("Points")
		if (points and points.Value >= cost) then
			if (backpack:FindFirstChild(item.Name)) then return end
			item:Clone().Parent = backpack
		end
	end
end

buyPart0.Touched:Connect(function(player)
	 handlePlayerPurchase(player, 500, speedCoil)
end)

buyPart1.Touched:Connect(function(player)
	 handlePlayerPurchase(player, 7000, gravityCoil)
end)

buyPart2.Touched:Connect(function(player)
	 handlePlayerPurchase(player, 10000, pointGiverTool)
end)

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