Is there a way to get local changed?

I tried creating a IntValue in my gun model, but the Value wasn’t changing somehow. Only reloading script somehow managed to get the actual IntValue. The reason I created IntValue instead of typing local ammo = 10 in a Script is because I wanted to use IntValue.Changed:Connect(function() but this did not work as I expected. Is there a way to get local changed?
Thanks for your help.

if the subtraction of Ammo’s is within the Client Boundary and you are using Server-Sided Scripts to Detect whether the value changes, It’s safe to say that the Client doesn’t replicated on the Server except for some movement

I’m changing the value on client and in this case I don’t care if exploiter changes the value. If you know how to detect local being changed, please tell me. You help will be appriecated!

pretty much the Function VALUE_OBJECT_HERE.Changed:Connect(...) would help specifically with your code


that would work if not then use a while loop or an GetPropertyChangedSignal() function type of thing

If your ammo system trusts the client to the extent of “I don’t care if exploiters grant themselves infinite ammo”, you should probably rework your code. Remember the “Golden Rule” of scripting - never trust the client. Important things like ammo count like this should be managed and checked on the server.

As for detecting change, .Changed:Connect() should work. Are you calling it properly? If your ammo is managed by the client, you need to call it clientside. Changes made by the client aren’t replicated to the server, so if the value gets changed clientside, the server won’t see it, thus not firing the event.

Val.Changed:Connect(function()
end)

doesn’t work in this case. I tried it before creating a topic. Here’s the error that you can get by doing .Changed:

[13:57:22.274 - Players.Jermartynojm.PlayerGui.LocalScript:3: attempt to index number with ‘Changed’
13:57:22.275 - Stack Begin
[13:57:22.276 - Script ‘Players.Jermartynojm.PlayerGui.LocalScript’, Line 3
13:57:22.276 - Stack End

Here’s the script that won’t work:

local int = 2

int.Changed:Connect(function()
print("Changed")
end)

wait(1)
int = 3

I know that this would work with a IntValue thing but that’s not what I want. I also do not really want to use while wait() do loop.

numbers don’t change actually you would need an BaseValue for this code to work actually

The code is made that exploiters even if changing the value of ammo are not going to achieve anything, so there’s no need to worry about that.

please don’t your performance will slow down if you do it

So you’re listening for .Changed on the server? As I said, if the value is changed on the client, it won’t be changed on the server. You need to call .Changed on the client if that’s where the value is being set.

There are no event’s on variables. Changed is a event available for object values. If you want to detect change on a variable, you can use a while loop.

local var = 3
 
while wait(1) do --> wait 1 second, 1/30th of a second IS not the good way
       if var ~= 3 then -- variable was changed
             print("var was changed")
       end
end

I don’t know if I said this, but I’m listening for Value change on client.

Why are you calling .Changed on a variable? You said you created an IntValue. An IntValue is an object - as @SilentsReplacement said, values don’t have events.

Lemme explain again. At the end of the topic I was asking: does the local value have .Changed event. The answer is that it doesn’t have .Changed or alternate event to check if it changed. At the begining I said that when I created IntValue inside my gun tool instead of using local, the changes of a value were only visible for reload Script. Even I was not able to see the changes in the explorer when I play tested the game. Why does only reload Script see the actual .Value property?

The error and code excerpt you posted shows you calling .Changed on a value. You’re not being very clear.

No, there is no .Changed for a “local” value as you call them. You’d be best off creating an IntValue, setting the value as your default ammo count and listening in for .Changed on that object.

If you set the value of an IntValue on the client, only the client will be able to see the updated value.

You’ve progressed past the original question of this post and it’s not very clear what you’re asking anymore.

Client changes are not replicated to the server, therefore the server cannot detect the changes.

The only solution is to change the value on the server via remote event, or set the network ownership of the IntValue to the client.

--// Local Script //

RemoteEvent:FireServer()
--// Server Script //

RemoteEvent.OnServerEvent:Connect(function(Player)
    local Gun = -- The gun
    local IntValue = -- The int value
    IntValue.Value = --The value
end)

Well if you want to track a value when it changes, sadly that is not possible. You’ll have to manually fire the function when it changes. Or just use your own solution, the .Changed event which is better.

I am looking around the model and you don’t refresh the text after reloading either. However, for security reasons, you really should be firing the server to reduce the ammo and such. The client should only listen for when it changes and change the ammo text.

I don’t care about the security because i’m making this gun for a game where players don’t need to reload their guns and they have infinite bullets. The second thing: when I was offline on DevForum, I realized that I have to do the same thing that you did in your post. Anyways, thanks for answering. I’ll mark your reply as a solution too.

1 Like

Oh alright! Glad that it works now.