Okay. So I made a part that when someone touches it, the intvalue goes up by 1 and when someone clicks on a button that value goes down by 1. The problem is that after someone clicked that button, the value goes down by 1, but after when someone touched the part to add to the value, it adds up 2 instead of 1. Here is the script in the part:
script.Parent.Touched:Connect(function(hit) if script.Parent.Reload.Value == false then if hit.Parent:FindFirstChild("Humanoid") ~= nil then if script.Parent.Players.Value < 5 then script.Parent.Reload.Value = true script.Parent.Players.Value = script.Parent.Players.Value +1 `the "Players" is the name of the value` hit.Parent.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.BallonPart.Position) local gui = game.ReplicatedStorage.ScreenGui:Clone() gui.Parent = game.Players:WaitForChild(hit.Parent.Name).PlayerGui end end end wait(2) script.Parent.Reload.Value = false end)
and here's the script in the button:
script.Parent.MouseButton1Click:Connect(function() game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.SpawnLocation.Position) game.Workspace.Part.Players.Value = game.Workspace.Part.Players.Value -1 script.Parent.Parent.Parent:Destroy() end)
I think that the 1st script is ignoring the changes made from the 2nd script, so it just keeps adding, but idk how to fix it. Altough the 2nd script isn't ignoring, because it is renewed every time (cloned and destroyed) so it forgets the value of the intvalue.
I'm sure that there is an way out of that and help will be appreciated!
you can set a value of an Int like this:
local intValue = game.bleh.bleh.etc intValue.Value = intValue.Value + 20 --this will add 20 to the vallue of `intValue` ------------or------------- local x = 5; intValue.Value = intValue.Value + x --this will add 5 to the value `IntValue`
you can set the value of an intValue
to a new value by doing
intValue.Value = 20
, <= this will make the value of intValue
20;
doing: intValue.Value = intValue.Value + z
basically tells the compiler this:
"hey I want you to add the value of z
to the value of intValue";
and similarly you can change the values of variables and properties of an object like this.
when you do number = number + X
with X
being a number, this tells the computer "take thee value of number and add the value of X
to the value it
however, doing number = X
, will not add the value of X
to number
, instead, the value of number
will be the value of X
its like saying "Hey make the value of number equal to the value of X"
example:
local number = 5; local object = {value = 2}; number = number + 5; --this adds 5 to the value of `number` print(number) --should print 10 in the output; object.value = object.value + 4 --adds 4 to the value of `object.value` print(object.value) --should print 6 number = 20 --sets the value of number to 20 print(number) -- should print 20 in the output.. object.value = 50 --sets object.value to 50 print(object.value) --should print 50