Hey, I was wondering how I could stop someone from using an auto clicker in my game?
I tried this, it didn’t work at all.
local btn = script.Parent
local chicken = game.ServerStorage.Beans
local holding = false
local canhold = true
btn.MouseButton1Down:Connect(function()
holding = true
while holding do
if canhold == true then
wait(0.5)
chicken:Clone().Parent = game.Workspace
end
btn.MouseButton1Up:Connect(function()
holding = false
canhold = false
wait(0.5)
canhold = true
end)
end
end)
You could do something like this as suggested above with a debounce:
local debounce = false
local cooldownTime = 0.1
local button = script.Parent
local chicken = game.ServerStorage.Beans
button.MouseButton1Click:Connect(function()
if not debounce then
local clone = chicken:Clone()
clone.Parent = game.Workspace
wait(cooldownTime)
debounce = false
end
end)
This code will clone the model into the workspace whenever the button is clicked. However, if the button is clicked to frequently (under the amount of cooldown time), the chicken will not be cloned.
You might want to use the new task.wait instead of the normal wait, though.
Well, I already tried something really close to that but it didn’t work, could you maybe show me how to use this method while mixed into my model cloning script. I can never seem to put things in the right order.
I can try to help, but I don’t know exactly what you are trying to do. It looks like you are cloning a model to the workspace repeatedly as a player holds the button, but there is a wait time in between. What do you intend for the button to do when clicked/held?
So… What exactly are you trying to do. Because I assume btn is a TextButton since you’re trying to use MouseButton1Down and MouseButton1Up. But you’re trying to access game.ServerStorage, from the client which you can’t do.
While the player holds down the button, every 0.5 seconds a chicken is cloned
local btn = script.Parent
local chicken = game.ServerStorage.Beans
local holding = false
btn.MouseButton1Down:Connect(function()
holding = true
while holding do
wait(0.5)
chicken:Clone().Parent = game.Workspace
end
btn.MouseButton1Up:Connect(function()
holding = false
end)
end
end)
I just changed your code a little and tested this and it works.
local btn = script.Parent
local chicken = game.ReplicatedStorage.Beans
local holding = false
local canhold = true
btn.MouseButton1Down:Connect(function()
holding = true
local con;
con = btn.MouseButton1Up:Connect(function()
holding = false
con:Disconnect();
end)
end)
local waitTime = 0.5;
local waited = 0;
game:GetService("RunService").Heartbeat:Connect(function(dt)
if holding then
waited += dt;
if waited >= waitTime then
chicken:Clone().Parent = workspace;
waited = 0;
end;
end;
end);
As I said before, you can’t access ServerStorage from the Client, nor can you access user input from the server. So you’re best off firing a remote to the server from the client when they should clone an object so that all players will see it.
Try something like this? You’ve almost got a working debounce system but there were some issues/fixes that I pointed out in comments
local btn = script.Parent
local chicken = game.ServerStorage.Beans
local holding = false
local canhold = true
btn.MouseButton1Down:Connect(function()
if holding then return end -- If we are already holding, stop
holding = true
while holding do
if canhold == true then
wait(0.5)
chicken:Clone().Parent = game.Workspace
end
end
end)
btn.MouseButton1Up:Connect(function() -- move this outside of the first button1down
holding = false
if canhold == false then return end -- don't run this timer more than needed
canhold = false
wait(0.5)
canhold = true
end)
Here’s the proper way to do something like this so all the chickens show up to every player
--Client
local btn = script.Parent
local chicken = game.ReplicatedStorage.RemoteEvent
local holding = false
btn.MouseButton1Down:Connect(function()
holding = true
local con;
con = btn.MouseButton1Up:Connect(function()
holding = false
con:Disconnect();
end)
end)
local waitTime = 0.5;
local waited = 0;
game:GetService("RunService").Heartbeat:Connect(function(dt)
if holding then
waited += dt;
if waited >= waitTime then
chicken:FireServer();
waited = 0;
end;
end;
end);
--Server
local event = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent");
local debounce = {};
event.OnServerEvent:Connect(function(player)
if not debounce[player] then debounce[player] = tick() - 0.5; end;
if (tick() - debounce[player]) < 0.5 then return; end;
debounce[player] = tick();
game:GetService("ServerStorage"):FindFirstChild("Beans"):Clone().Parent = workspace;
end);
Note: Security wise though, you should be checking on the server when a player can spawn a chicken so that exploiters can’t just constantly fire your remote.
we’ll it worked for the most part but I think it interfered with something else. The button kind of acted as a toggle on/off. It worked earlier, and I know it was probably my fault it is not working now. But I didn’t understand some of it so instead of editing it, I used a different script. But when it worked it worked like a charm. I just think that another one of my scripts must of interfered with it or something.