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)