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

Tool script not checking if the value has updated?

Hello.

I am currently trying to make a very simple script that can check if a Value has changed on ReplicatedStorage (as a test) to work as a stopper of some sort. I've made a simple script for it and it works, however it only checks once, and it's when the player joins, and after any value checks it always remains with the first pick. Say if the value was false at the start, it would run the code if the value was false, but if the value got changed to true afterwards, it would still run the code if the value was false. I seriously cannot wrap my head around this and I need some help.

Here is the simple code:

local Tool = script.Parent
local RP = game:GetService("ReplicatedStorage")

Tool.Activated:Connect(function()
		if RP.Value.Value == true then
			print("Value is true")
		end
		if RP.Value.Value == false then
			print("Value is false")
		end
end)

If I could get some help with this it would be great.

Thanks!

4 comments

0

Local Script or Server Script?

0

ServerScript

0

Did you change the value to true using a different script or by using Roblox Studio during testing?

0

No, I used the tool itself to change the value, as you can see from the script, as the tool is activated it runs, I also did this using Roblox Studio during testing

1 answer

✓ Accepted answer

The answer is pretty simple and probably complicated, you are countering this problem because you are using a serverscript to check something that is client-sided, the Value is in ReplicatedStorage which will replicate the value only to the client, which only a LocalScript will detect on the clients' side, since you are using a serverscript, it will not.

Ok so, this might be hard to understand but if your value was automatically false then it will keep printing the value is false forever unless the value changes from the server.

Example:

Let's say we have the value set to false in the Replicated Storage:

Value Image

And we have a tool in starterpack with a serverscript will the following:

Tool Image

local RP = game:GetService("ReplicatedStorage")

script.Parent.Activated:connect(function()
	if RP.Value.Value == false then
		print("It is false") 
	else
		print("it is true")
	end
end)
--Output: false

It will keep printing false even if we set the value to true on the explorer tab.

So how do we fix this?

Well, for one, we can use a LocalScript with the same script inside of it Image to check if it changed on the clients side, or servers side.

So if we do change the value, it will start to print true.

Now let's say you still want to keep it a serverscript for whatever reason, then that means you will have to change the value on the servers side. How? Well, you can add a serverscript which has the following code to change it:

wait(5)
print('changed')
game.ReplicatedStorage.Value.Value = true

Now your tool will actually get the change and start to print true.

1 comment

0

This worked very well. Thank you!

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