Sn1perA
(Ace)
June 15, 2022, 12:07am
1
Hello devs.
I created a script that when the player clicks on a proximity prompt it gives him two options to a car, cancel or buy.
But for some reason every time I click on buy it doubles the values, as you can see in the video below:
Server Script:
AreYouSureRE.OnServerEvent:Connect(function(player,carName,price)
if player.Money.Value >= price then player.Money.Value -= price
local CarValue = Instance.new("StringValue"); CarValue.Name = carName; CarValue.Parent = player.PlayerCarsInfoFolder.Cars
end
end)
Client:
PPService.PromptTriggered:Connect(function(proximity, player)
if proximity.Name == "BuyCar" then
Main.Visible = true
AreYouSureLabel.Text = "Are You sure You wan't to Buy " .. proximity.Parent.Parent.Name .. "?"
Buy.MouseButton1Click:Connect(function()
Main.Visible = false
AreYouSureRE:FireServer(proximity.Parent.Parent.Name, proximity.Parent.Parent.Price.Value)
end)
Cancel.MouseButton1Click:Connect(function()
Main.Visible = false
end)
end
end)
Iāve tried everything but it still doesnāt work, what do you guys suggest?
Try adding a debounce:
local debounce = false
AreYouSureRE.OnServerEvent:Connect(function(player,carName,price)
if player.Money.Value >= price and debounce == false then
player.Money.Value -= price
local CarValue = Instance.new("StringValue"); CarValue.Name = carName; CarValue.Parent = player.PlayerCarsInfoFolder.Cars
debounce = true
wait(0.5)
debounce = false
end
end)
Sn1perA
(Ace)
June 15, 2022, 12:17am
3
Even with debounce didnāt work
made me think, does the problem starting on the client?
Kevblx
(Kev)
June 15, 2022, 12:18am
4
Did you try disconnecting the mouseclick event?
1 Like
For some reason your client must be double-firing the action. Try placing the debounce on the client instead.
Sn1perA
(Ace)
June 15, 2022, 12:19am
6
I just realized something⦠the debounce was inside the event, it worked now on the outside!
Wait, I figured it out⦠(code will show upon edit)
oh, nevermind
Sn1perA
(Ace)
June 15, 2022, 12:21am
8
but anyway thank you very much
Nevertheless, you can still make this code standalone:
Buy.MouseButton1Click:Connect(function()
Main.Visible = false
AreYouSureRE:FireServer(proximity.Parent.Parent.Name, proximity.Parent.Parent.Price.Value)
end)
Cancel.MouseButton1Click:Connect(function()
Main.Visible = false
end)
1 Like
Sn1perA
(Ace)
June 15, 2022, 12:23am
10
Do you think putting a debounce is the best solution for this kind of problem?
But, make sure to add statements to check if itās gonna work, too!
1 Like
Thinking about it, thereās probably a better solution. But for now, Iād just say that a debounce will work for now.
1 Like
Sn1perA
(Ace)
June 15, 2022, 12:27am
13
Yes, maybe i can find a better solution using āMousebutton1Clickā and āProximity Promptsā in the same script. But anyway thank you very much
Kevblx
(Kev)
June 15, 2022, 12:27am
14
Everytime the proximity prompt is clicked, it adds another mouseclicked event, your debounce just stopped the second fire. Way to check this is to put a print in the mouse click, see on the second time if you buy it if it prints twice
Sn1perA
(Ace)
June 15, 2022, 12:32am
15
This makes sense, the solution for this is to leave the buy and cancel options outside the if?
Kevblx
(Kev)
June 15, 2022, 12:34am
16
Let me know if this works!
local MouseEvent
PPService.PromptTriggered:Connect(function(proximity, player)
if proximity.Name == "BuyCar" then
Main.Visible = true
AreYouSureLabel.Text = "Are You sure You wan't to Buy " .. proximity.Parent.Parent.Name .. "?"
MouseEvent = Buy.MouseButton1Click:Connect(function()
MouseEvent:Disconnect()
Main.Visible = false
AreYouSureRE:FireServer(proximity.Parent.Parent.Name, proximity.Parent.Parent.Price.Value)
end)
Cancel.MouseButton1Click:Connect(function()
Main.Visible = false
end)
end
end)
1 Like
Sn1perA
(Ace)
June 15, 2022, 12:40am
17
Wow, wow and wow! This is actually the solution.
as expected, firing only once
I really appreciate!
1 Like
Forummer
(Forummer)
June 15, 2022, 12:49pm
18
You were stacking event connections without appropriately disconnecting the previous connection (whenever the promptās āPromptTriggeredā signal fired).
It is important to remember that connections stack (they do not override).
local Game = game
local Players = game:GetService("Players")
local function OnPlayerRemoving(Player)
print(Player.Name) --Prints the leaving player's name 100 times.
end
for _ = 1, 100 do
Players.PlayerRemoving:Connect(OnPlayerRemoving) --Connection is created 100 times ('OnPlayerRemoving' callback function is connected to the player's 'PlayerRemoving' signal 100 times).
end
1 Like