Sending a value from the server to the client and then printing it when a player presses a button in

heres the problem , i dont know whats happening but when i send a number value called energy to the client, i want it so when the player has pressed a button it prints the value, but the probelm is that it prints the old and new value for some reason.
i have an another script that substracts 10 from the energy value every second , but when the client presses the button and it prints the energy value it shows as 20 and 30 even thought the value currently is 20 so why is it printing the old and new value i dont know

heres the server script

game.Players.PlayerAdded:Connect(function(plr)
	
	
	wait()
	plr.PlayerNeeds.Energy:GetPropertyChangedSignal("Value"):Connect(function()
		
	game.ReplicatedStorage.StreamingEvents.CheckEnergy:FireClient(plr, plr.PlayerNeeds.Energy.Value)

	end)

end)

and heres the client script

game.ReplicatedStorage.StreamingEvents.CheckEnergy.OnClientEvent:Connect(function(EnergyVal)
	
	script.Parent.MouseButton1Click:Connect(function()
	
	print(EnergyVal)
		
	end)
	
end)


9 Likes

i think its cause the firing line hasent been indented properly, try moving it up a tab

8 Likes

sorry, but im not sure what you mean by up a tab ?

7 Likes

Press Tab at the start of the line of code which fires the arguments to the client.

Also are you sure you are changing the value of the energy on the server?

4 Likes

yes im changing the energy value from the server not the client

4 Likes

by the way the probelm only happens when i add the line

script.Parent.MouseButton1Click:Connect(function()

but otherwise it works perfectly fine it prints the exact value of the energy and not the old values

5 Likes

That’s because printing the values is delayed. Both the 20 and the 30 value are being stored and waiting for the button to be clicked, and only then will they print. This means that both will print at the same time and you’ll be left with the output “20” and the output “30”. From what I can gather you only want the value printed when the button is clicked. To do this try checking the player’s energy only when the button is clicked with a local script like this:

local plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	
	local energyval = plr.PlayerNeeds.Energy.Value -- checks value only when the button is clicked

	print(energyval)

end)
5 Likes

but the problem with the script you gave me is that it checks from the client , this means that an exploiter can change the energy value from the client and the script will not care because its checking it locally and not from the server, the reason i want it to check in the server is because of roblox filtering,i knew this solution but i want to make my game secure, thanks for the help in advance

2 Likes

Ahh I totally forgot about exploiters haha. You can try updating a variable when the remote is fired to transfer the value, just make sure the mouse click function is not inside the onClientEvent one. You can add this to the previous script I suggested:

local energyval = 0

game.ReplicatedStorage.CheckEnergy.OnClientEvent:Connect(function(EnergyVal)
	energyval = EnergyVal
end)

I’m not sure if this is the most efficient way to do this, but it works from what I’ve tested. There are tons of resources online you can check out as well!

4 Likes

thank you very much for your help !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.