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

How do I detect a change in a value with a script?

I need a tool to detect a change of a value in Workspace, but the script won't detect it, any help?

while true do
	if game.Workspace.Value.Value == 1 then
		script.Parent.Part.Material = Enum.Material.Neon
		
	else
		script.Parent.Part.Material = Enum.Material.Plastic
	end
	wait(1)
end

This is the script I've used, won't work.

2 answers

✓ Accepted answer

A Better Way To Do This Would Be To Use The Instance:GetPropertyChangedSignal. This Allows you to run a function when a specific property has been changed. For Example:

local value = workspace.Value --Just An Example
local Part = script.Parent --The Actual Script's Parent
value:GetPropertyChangedSignal("Value"):Connect(function() 	--Takes Name Of Property For Parameter
	if value.Value == 1 then --If Equal to 1 then
		Part.Material = Enum.Material.Neon
	else --if not
		Part.Material = Enum.Material.Plastic
	end
end)

I've tested this and it works perfectly, uses much less resources than constantly checking.

Hope this helps

8 comments

0

If within a tool, use the activated event like BlackBooks did and fire the server

0

It didnt work for me

0

Because the value isn't 1, switch to server mode and change the value to 1

0

I thought you could change the things without using server mode, thanks

0

yeah you can't because of filtering enabled, stops clients editing things on server side

0

tell me if it works

0

it does

0

k thats good

look at your type of script, you should use script instead of local script. And make sure that you're changing the value so the material could be changing

if you want to make a tool then you need to use local script and use fireserver() here is the example localscript inside tool

script.Parent.Activated:Connect(function() --this event triggered when you click the tool
	workspace.RemoteEvent:FireServer()
end)

script inside serverscriptservice

workspace.RemoteEvent.OnServerEvent:Connect(function()
		if game.Workspace.Value.Value == 1 then --when the tool is clicked and the value is 1 then it will change the material
			workspace.Part.Material = Enum.Material.Neon --insert your part here
		else
			workspace.Part.Material = Enum.Material.Plastic --insert your part here
		end
end)

let me know if it helps

2 comments

0

So, do I have to make a RemoteEvent?

0

yes of course

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