Giving power even if i click on gui

Want to give a player more power if they click without a tool. So I use the UserInputService.

I just want it to give power when you click in the air and not on a GUI etc.

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local ToolValue = player.ToolValue.Value
		player.leaderstats.Power.Value = player.leaderstats.Power.Value + ToolValue * player.rank.Value
	end
end)

You haven’t specifically said what you need help with, however what I’ve noticed is that you’re trying to change a leaderstats value through the client.

Any changes like this made on the client will not replicate to the server, therefore the power will only go up on your client and no one else’s.

To achieve what you want, you’ll need to make use of RemoteEvents.

What you need to make sure is that you never trust the client. RemoteEvents can be fired by exploiters, which can lead to unwanted outcomes. Make sure you do necessary checks on the server.

Heres a demonstration:

Bad example
event.OnServerEvent:Connect(function(PlayerWhoFired,arg)
	local ToolValue = PlayerWhoFired.ToolValue.Value
	PlayerWhoFired.leaderstats.Power.Value = PlayerWhoFired.leaderstats.Power.Value + arg
end)

In the example above, you are letting the client decide how much the power value goes up by, which means an exploiter can easily change that value to anything.

Good example
event.OnServerEvent:Connect(function(PlayerWhoFired,arg)
	local ToolValue = PlayerWhoFired.ToolValue.Value
	PlayerWhoFired.leaderstats.Power.Value = PlayerWhoFired.leaderstats.Power.Value + ToolValue * PlayerWhoFired.rank.Value
end)

In the above example, the server decides the correct amount for the power value to go up by, which means it cannot be exploited. However, I also suggest you add some kind of cooldown so it cannot be spammed by an exploiter.

1 Like

The second argument of InputBegan is gameProcessed. If you want it to function if a person clicks but not on a GUI, it has to be false. I see that you increase the player’s leaderstats locally. Only the player will see and it won’t replicate to the server. Usage of remote events will be required.

3 Likes
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character

uis.InputBegan:Connect(function(input, gameProcessedEvent)

	gameProcessedEvent = false
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local ToolValue = player.ToolValue.Value
		player.leaderstats.Power.Value = player.leaderstats.Power.Value + ToolValue * player.rank.Value
	end
end)

Now I’ve set it to false but it does still give me power when I click the gui.

And to the Server and Client thing, I’m just trying to fix the GUI thing now and then fix the remote event. I’m sorry that i didn’t say that earlier.

You need to use an if statement.

uis.InputBegan:Connect(function(input, gameProcessedEvent)

	if not gameProcessedEvent then
2 Likes