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

Why Wont It Change Colors When Touched?

local function onTouch(hit)
	
	game.Workspace.Part.BrickColor = BrickColor.new("Mid gray")
end

No errors?

1 comment

0

Also make sure it is in a server script.

1 answer

Problem: (Re-edited - August 18/08/2021)

  • You did not call the function
  • There's no touched event, to detect whether a player has touched something

Solution/Description:

Make sure you call the function or the function would not know what to do since you haven't actually called it.

Add a Touched event to detect whether a player has actually touched a part, because, by your question, I'm pretty sure that's what you're trying to accomplish!

Recommendation(s)

It's also best to use a debounce, to prevent the code from running too many times!

Fixed Script:


local debounce = true
local part = workspace.Part
local function onTouch(hit)
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce then
			debounce = false
			wait(1)
			part.BrickColor = BrickColor.new("Mid gray")
			debounce = true
		end
	end)
end
onTouch() -- calling the function

Place this script in the Workspace aswell as the part, and you should be set!

I normally don't edit answers like this, especially if they're over one year old, but I'm editing this because of grammatical issues, better description, and improved code for newer developers.

2 comments

0

it didnt work error15:54:25.647 - Workspace.Part.Script:6: Expected 'end' (to close 'function' at line 2), got ; did you forget to close 'then' at line 3?

0

edited try again.

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