Spill+Mop system script not working

I joined the game and all the spills are transparent with a ProximityPrompt in each and nothing works either. can someone help fix my script?

local GroupId = 5901406
local RequiredRank = 10

local workspace = game:GetService("Workspace")
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")

local function setVisible(spillFolder, spillNumber, visible)
	local spill = spillFolder:FindFirstChild("Spill" .. spillNumber)
	if spill then
		for _, child in pairs(spill:GetChildren()) do
			if child:IsA("MeshPart") then
				if child.Name == "Lid" or child.Name == "IceGiver" then
					child.Transparency = visible and 0.55 or 1
				else
					child.Transparency = visible and 0 or 1
				end
			end
		end
		return spill
	end
end

local function createPrompt(spill)
	local prompt = workspace.Spills.ProximityPrompt:Clone()
	prompt.Parent = spill
	return prompt
end

local function onPromptTriggered(player, prompt, mopTool)
	local character = player.Character
	if not character then return end

	local humanoid = character:FindFirstChild("Humanoid")
	if not humanoid then return end

	humanoid:LoadAnimation(14607609110):Play()

	mopTool:Clone().Parent = player.Backpack

	wait(5)

	mopTool:Destroy()
	prompt:Destroy()

	for _, child in pairs(player.Character:GetChildren()) do
		if child:IsA("Tool") and child.Name == "Mop" then
			child:Destroy()
		end
	end

	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local stars = leaderstats:FindFirstChild("Stars")
		if stars and stars:IsA("IntValue") then
			stars.Value = stars.Value + 1
		end
	end
end

local function main()
	local spillFolder = workspace:FindFirstChild("Spills")
	if not spillFolder then
		return
	end

	local proximityPrompt = spillFolder:FindFirstChild("ProximityPrompt")
	if not proximityPrompt then
		return
	end

	local mopTool = serverStorage:FindFirstChild("Mop")
	if not mopTool then
		return
	end

	while true do
		local spillNumber = math.random(1, 15)
		local spill = setVisible(spillFolder, spillNumber, true)
		if spill then
			local proxPart = spill:FindFirstChild("ProxPart")
			if proxPart then
				proxPart.Transparency = 1
				local prompt = createPrompt(proxPart)
				local player = players:GetRandomPlayer()
				if player then
					if player:IsInGroup(GroupId) then
						local playerRank = player:GetRankInGroup(GroupId)
						if playerRank >= RequiredRank then
							prompt.Triggered:Connect(function()
								onPromptTriggered(player, prompt, mopTool)
							end)
						end
					end
				end
				wait(60)
				setVisible(spillFolder, spillNumber, false)
			end
		end
	end
end

runService.Heartbeat:Connect(main)

Are there any errors in the Output window in Studio? It would also be good to have a screenshot of the structure of one of the items in the spillFolder to see how they are setup.
In your current scripts, I see a couple of issues:

  1. child.Transparency show just be a numerical value, so the visible value set is invalid
  2. runService.Heartbeat:Connect(main) us running the main function every heartbeat. That function is then creating the prompt.Triggered:Connect everytime it runs, I expect that will be a causing a memory leak time and is unnecessary.
    Send us the errors form the Output window, that will be the best place to start
1 Like

This is a pretty hard script to try to dissect, and I made a bunch of assumptions:

  • That the spills in the spills folder are models with parts inside
  • That there already exists SpillFolder in workspace
  • That ProximityPrompt exists in SpillFolder
  • That Mop already exists in serverStorage
  • That you wanted spills to spawn every 60 seconds

With my assumptions hopefully being correct,
here’s my shot at trying to fix the script:

local GroupId = 5901406
local RequiredRank = 10

local workspace = game:GetService("Workspace") --// This line is unnecessary, but if you want it you can leave it
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")

local proxPromptService = game:GetService("ProximityPromptService") --// Service that proximity prompts use and communicate with

local spillFolder = workspace:FindFirstChild("Spills")

--// This function is fine
local function setVisible(spillFolder, spillNumber, visible)
	local spill = spillFolder:FindFirstChild("Spill" .. spillNumber)
	if spill then
		for _, child in pairs(spill:GetChildren()) do
			if child:IsA("MeshPart") then
				if child.Name == "Lid" or child.Name == "IceGiver" then
					child.Transparency = visible and 0.55 or 1
				else
					child.Transparency = visible and 0 or 1
				end
			end
		end
	end
	return spill
end

local function createPrompt(spill)
	local prompt = workspace.Spills.ProximityPrompt:Clone()
	prompt.Name = "SpillPrompt" --// Identity for the prompt
	prompt.Parent = spill
	return prompt
end

local function onPromptTriggered(player : Player, prompt : ProximityPrompt, mopTool : Tool)
	local character = player.Character
	if not character then return end

	local humanoid = character:FindFirstChild("Humanoid")
	if not humanoid then return end
	
	if prompt.Name ~= "SpillPrompt" then return end

	humanoid:LoadAnimation(14607609110):Play()
	
	local mopClone = mopTool:Clone()
	mopClone.Parent = player.Backpack

	wait(5)
	--// Don't destroy the mopTool in ServerStorage, or else we won't be able to clone it
	mopClone:Destroy()
	
	--// Rather than destroying the prompt, we disable it so that we can use it again
	--prompt:Destroy()
	prompt.Enabled = false
	
	local spill = prompt:FindFirstAncestorOfClass("Model")
	local spillNumber = string.sub(spill.Name, 6) --// gets the number after "Spill"
	setVisible(spillFolder, spillNumber, false)

	for _, child in pairs(player.Character:GetChildren()) do
		if child:IsA("Tool") and child.Name == "Mop" then
			child:Destroy()
		end
	end

	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local stars = leaderstats:FindFirstChild("Stars")
		if stars and stars:IsA("IntValue") then
			stars.Value = stars.Value + 1
		end
	end
end

proxPromptService.PromptTriggered:Connect(function(prompt, player)
	local mopTool = serverStorage:FindFirstChild("Mop")
	if not mopTool then warn("No mop tool in ServerStorage") return end
	if player:IsInGroup(GroupId) then
		local playerRank = player:GetRankInGroup(GroupId)
		if playerRank >= RequiredRank then
			onPromptTriggered(player, prompt, mopTool)
		end
	end
end)

-- The main function is unnecessary in my opinion

--// Setup the spills by making the proxPart transparent and adding the prompt
for _, spill in workspace.Spills:GetChildren() do
	if spill:IsA("Model") then
		local proxPart = spill:FindFirstChild("ProxPart")
		if proxPart then
			proxPart.Transparency = 1
			local prompt = createPrompt(proxPart)
		end
	end
end

task.spawn(function() --// task.spawn() allows for code past this while loop to run
	
	--// This while true loop should make a random spill visible every minute / 60 seconds
	while true do
		local spillNumber = math.random(1, 15)
		local spill = setVisible(spillFolder, tostring(spillNumber), true)
		if spill then
			local prompt = spill:FindFirstChild("SpillPrompt", true) --// The second arguement for FindFirstChild allows for
																	-- a recursive search, searching children of children
			prompt.Enabled = true
		end
		wait(60)
	end
end)

I have various comments around the code trying to explain the changes I made, so hopefully you don’t just copy and paste the code, but read through it and understand why I made the changes I did.

1 Like

This is the explorer panel

Okay, that looks about what I thought it would, have you tried the script?

1 Like

i will soon, ill let you know once i tried it thank you

Ok so the 60 seconds wait before a spill shows up part works, but the ProximityPrompt is showing in all of them and even on the spills that are visible, nothing happens when i trigger the ProximityPrompt

Okay, for the proximity prompts, disable them after they’ve been created, whether in the createPrompt function or in the for loop on line 92.

Changing the createPrompt function:

local function createPrompt(spill)
	local prompt = workspace.Spills.ProximityPrompt:Clone()
	prompt.Name = "SpillPrompt" --// Identity for the prompt
	prompt.Parent = spill
	prompt.Enabled = false
	return prompt
end

OR
on line 98:

for _, spill in workspace.Spills:GetChildren() do
	if spill:IsA("Model") then
		local proxPart = spill:FindFirstChild("ProxPart")
		if proxPart then
			proxPart.Transparency = 1
			local prompt = createPrompt(proxPart)
			prompt.Enabled = false
		end
	end
end

As for the prompts not doing anything, remember that you have the group rank if statement. Make sure you are actually a high enough rank (you probably are, but you never know) or you can comment those checks for testing purposes.

1 Like

Im pretty sure the rank part is correct because the group id and minimum rank id are correct. But i will try it out rn!