Change value RemoteEvent

Is there a better way of doing this?

LocalScript #1:

Button.MouseButton1Click:Connect(function()
	Event:FireServer(1) 
end)

LocalScript #2:

Button.MouseButton1Click:Connect(function()
	Event:FireServer(2) 
end)

LocalScript #3:

Button.MouseButton1Click:Connect(function()
	Event:FireServer(3) 
end)

Script:

PrimaryWeaponEvent.OnServerEvent:Connect(function(player, Value)
	if Value == 1 then
		CoolValue.Value = 1
	elseif Value == 2 then
		CoolValue.Value = 2
	elseif Value == 3 then
		CoolValue.Value = 3
	end
end)

I would say that there’s not much else you can do to make sure CoolValue gets the proper value. What you are doing is very common and basic. I wouldn’t be surprised if there were other alternatives to this.

In short, no.

I just noticed that this is a bit unoptimized. Try clamping and rounding the value. math.clamp makes it so the number given is within a range, and math.floor basically cuts out the decimal part of the value.

PrimaryWeaponEvent.OnServerEvent:Connect(function(player, Value)
	Value = math.floor(math.clamp(Value, 1, 3))
	CoolValue.Value = Value
end)
2 Likes

Pardon, I forgot to fix the line that sets the value instance to a new number.

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