So, I have a GUI-Shop where you can click on an arrow to the right to switch the purchasable item and one arrow to the right.
I am new to scripting so I guess the code is not that good
All things are working until I close and then reopen the shop.
If I do that the Int value jumps (or lowers) by two when clicking on the arrow
Here is my code:
local frame = script.Parent.Frame
local PPS = game:GetService("ProximityPromptService")
local plr = game.Players.LocalPlayer
local forward = script.Parent.Frame.Forward
local backward = script.Parent.Frame.Backward
local AR = frame.AR
local Pistol = frame.Pistol
local SMG = frame.SMG
local Shotgun = frame.Shotgun
local item = frame.Item
local close = script.Parent.Frame.CloseButton
PPS.PromptTriggered:Connect(function(prompt, player)
local pl = player.Name
print (pl)
if prompt.Name == "OpenGunShopGuiPrompt" then
frame.Visible = true
Pistol.Visible = true
script.Page.Value = 1
item.Text = "Pistol"
print"Pistol p.1"
end
forward.MouseButton1Click:Connect(function()
if script.Page.Value == 1 then
script.Page.Value = 2
Pistol.Visible = false
SMG.Visible = true
item.Text = "SMG"
print"SMG p.2"
else if script.Page.Value == 2 then
script.Page.Value = 3
SMG.Visible = false
Shotgun.Visible = true
item.Text = "Shotgun"
print"Shotgun p.3"
else if script.Page.Value == 3 then
script.Page.Value = 4
Shotgun.Visible = false
AR.Visible = true
item.Text = "AR"
print"AR p.4"
else if script.Page.Value == 4 then
script.Page.Value = 1
AR.Visible = false
Pistol.Visible = true
item.Text = "Pistol"
print"Pistol p.1"
end end end end end)
backward.MouseButton1Click:Connect(function()
if script.Page.Value == 1 then
script.Page.Value = 4
Pistol.Visible = false
AR.Visible = true
item.Text = "AR"
print"AR p.4"
else if script.Page.Value == 2 then
script.Page.Value = 1
SMG.Visible = false
Pistol.Visible = true
item.Text = "Pistol"
print"Pistol p.1"
else if script.Page.Value == 3 then
script.Page.Value = 2
Shotgun.Visible = false
SMG.Visible = true
item.Text = "SMG"
print"SMG p.2"
else if script.Page.Value == 4 then
script.Page.Value = 3
AR.Visible = false
Shotgun.Visible = true
item.Text = "Shotgun"
print"Shotgun p.3"
end end end end end)
close.MouseButton1Click:Connect(function()
AR.Visible = false
SMG.Visible = false
Pistol.Visible = false
Shotgun.Visible = false
frame.Visible = false
script.Page.Value = 0
end)
end)
PPS.PromptTriggered:Connect(function(prompt, player)
local pl = player.Name
print (pl)
if prompt.Name == "OpenGunShopGuiPrompt" then
frame.Visible = true
Pistol.Visible = true
script.Page.Value = 1
item.Text = "Pistol"
print"Pistol p.1"
end
end) --End was missing here--
forward.MouseButton1Click:Connect(function()
if script.Page.Value == 1 then
script.Page.Value = 2
Pistol.Visible = false
SMG.Visible = true
item.Text = "SMG"
print"SMG p.2"
else if script.Page.Value == 2 then
script.Page.Value = 3
SMG.Visible = false
Shotgun.Visible = true
item.Text = "Shotgun"
print"Shotgun p.3"
else if script.Page.Value == 3 then
script.Page.Value = 4
Shotgun.Visible = false
AR.Visible = true
item.Text = "AR"
print"AR p.4"
else if script.Page.Value == 4 then
script.Page.Value = 1
AR.Visible = false
Pistol.Visible = true
item.Text = "Pistol"
print"Pistol p.1"
end end end end end)
backward.MouseButton1Click:Connect(function()
if script.Page.Value == 1 then
script.Page.Value = 4
Pistol.Visible = false
AR.Visible = true
item.Text = "AR"
print"AR p.4"
else if script.Page.Value == 2 then
script.Page.Value = 1
SMG.Visible = false
Pistol.Visible = true
item.Text = "Pistol"
print"Pistol p.1"
else if script.Page.Value == 3 then
script.Page.Value = 2
Shotgun.Visible = false
SMG.Visible = true
item.Text = "SMG"
print"SMG p.2"
else if script.Page.Value == 4 then
script.Page.Value = 3
AR.Visible = false
Shotgun.Visible = true
item.Text = "Shotgun"
print"Shotgun p.3"
end end end end end)
close.MouseButton1Click:Connect(function()
AR.Visible = false
SMG.Visible = false
Pistol.Visible = false
Shotgun.Visible = false
frame.Visible = false
script.Page.Value = 0
end)
--The End for the connection statement was all the way down here, causing the entire script to be re-ran every time you opened the GUI.
The issue is here that there is a missing end on the PPS statement, and because of this, every time you execute the PPS, your connecting another connection. Causing it to run twice, and if you do it again, it will run three times. Just adding the missing end will fix this problem. (But please clean up your code, some ideas could be using an array to store items and accessing it there.)
Thanks! The script works fine now.
Edit: Yeah, I know my code is really messy, but I can’t figure out how exactly to clean it up. The only thing I know is how to use variables