Issues with working system

Hello,
I’m currently trying to make a shop system in which the player can work in, as soon as the player steps behind the cash register, it will spawn in an NPC and the NPC will walk towards the cash register. The NPC will now tell the player that it wants to buy one of three colors, which are: red, green and black. Now my problem is that whenever the player choses a wrong color, it’s just like nothing happens. Also, even if the player choses the correct color and the NPC gets destroyed, the script doesn’t start over again. I have also rewrote the script multiple times, and this is the one that is working the best so far. Thanks to whoever helps in advance.

Code:

local npc = game.ReplicatedStorage.NPC
local chat = game:GetService("Chat")
local Player = game:GetService("Players")
local randomColor = math.random(1, 3)
local workerName = nil
local spawned = false

script.Parent.Parent.Parent.InvisiblePart.Touched:Connect(function(hit)
	local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent)
	local leaderstats = PlayerCharacter:WaitForChild("leaderstats")
	if leaderstats then
		local money = leaderstats:FindFirstChild("Money")
		if spawned == false then
			spawned = true
			workerName = hit.Parent.Name
			local npcSpawn = npc:Clone()
			npcSpawn.HumanoidRootPart.Position = Vector3.new(-251.071, 0.5, -52.072)
			npcSpawn.Parent = game.Workspace
			npcSpawn.Name = "Customer"
			print(npcSpawn.Name)
			print("NPC spawned")
			wait(5)
			local newNpc = game.Workspace:FindFirstChild("Customer")
			script.Parent.Worker.Value = hit.Parent.Name
			if randomColor == 1 then
				script.Parent.Color.Value = "Red"
				chat:Chat(newNpc.Head, "hello i want red")
				script.Parent.Red.ClickDetector.MouseClick:Connect(function()
					if script.Parent.Color.Value == "Red" then
						chat:Chat(newNpc.Head, "thank you!")
						money.Value += 50
						script.Parent.Color.Value = "nil"
						wait(3)
						newNpc:Destroy()
					elseif script.Parent.Color.Value ~= "Red" then
						chat:Chat(newNpc, "thats not what i wanted")
						newNpc:Destroy()
						script.Parent.Color.Value = "nil"
					end
				end)
			elseif randomColor == 2 then
				script.Parent.Color.Value = "Green"
				chat:Chat(newNpc.Head, "hello i want green")
				script.Parent.Green.ClickDetector.MouseClick:Connect(function()
					if script.Parent.Color.Value == "Green" then
						chat:Chat(newNpc.Head, "thank you!")
						money.Value += 50
						script.Parent.Color.Value = "nil"
						wait(3)
						newNpc:Destroy()
					elseif script.Parent.Color.Value ~= "Green" then
						chat:Chat(newNpc, "thats not what i wanted")
						newNpc:Destroy()
						script.Parent.Color.Value = "nil"
					end
				end)
			elseif randomColor == 3 then
				script.Parent.Color.Value = "Black"
				chat:Chat(newNpc.Head, "hello i want black")
				script.Parent.Black.ClickDetector.MouseClick:Connect(function()
					if script.Parent.Color.Value == "Black" then
						chat:Chat(newNpc.Head, "thank you!")
						money.Value += 50
						script.Parent.Color.Value = "nil"
						wait(3)
						newNpc:Destroy()
					elseif script.Parent.Color.Value ~= "Black" then
						chat:Chat(newNpc, "thats not what i wanted")
						newNpc:Destroy()
						script.Parent.Color.Value = "nil"
					end
				end)
			end
		end
	end
end)

Hi,

I had a similar system in one of my games and I used events to ensure that the script kept repeating and it worked just fine, but I had to take extra steps to prevent against exploits. My game was also completely centered around that type of NPC interaction so I didn’t need to worry about performance, but it might be different in your case.

As for the wrong color issue, I would probably replace the “elseif” statement with just “else” but I don’t know if that’s the issue. Also, you could try putting the colors in an array and then the color could just be array[randomNumber] and then you could just have one if statement checking if the color the player chose matches the color generated. This system would reduce the number of lines of code and make it easier for you to find bugs in the future.

Hope this helps!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.