Hello,
I have a problem with the camera changing positions when equip item
video:
Whenever I equip an item the camera position is set to 1, why?
script:
local Remotes = game.ReplicatedStorage.Remotes
local Camera = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local Gui = script.Parent
local OnSlot = 1
local ItemsInfo = {
GrassLand = {
["FarmerHat"] = {
["Name"] = "Farmer Hat",
["Cost"] = 200,
["Boost1"] = "+2 to money boost on grassland",
},
["CactusHat"] = {
["Name"] = "Cactus Hat",
["Cost"] = 5000,
["Boost1"] = "+2 to money boost on Desert",
}
}
}
local ShopVar
local CamsVar
function OpenShop(CamPos, Shop)
ShopVar = Shop
Camera.CameraType = Enum.CameraType.Scriptable
local Cams = CamPos:GetChildren()
table.sort(Cams, function(a,b)
return a.Name < b.Name
end)
CamsVar = Cams
Camera.CFrame = Cams[1].CFrame
SetItemInfo()
end
function CloseShop()
Camera.CameraType = Enum.CameraType.Custom
Camera.CFrame = plr.Character.HumanoidRootPart.CFrame
OnSlot = 1
Gui.BuyBackground.Visible = true
Gui.EquipBackground.Visible = false
end
function SetItemInfo()
local Children = ShopVar.Items:GetChildren()
table.sort(Children, function(a,b)
return a.Name < b.Name
end)
local Item = Children[OnSlot]
if Item.Unlocked.Value then
Gui.BuyBackground.Visible = false
if Item.Equiped.Value then
Gui.UnEquipBackground.Visible = true
Gui.EquipBackground.Visible = false
else
Gui.UnEquipBackground.Visible = false
Gui.EquipBackground.Visible = true
end
else
Gui.BuyBackground.Visible = true
Gui.EquipBackground.Visible = false
Gui.UnEquipBackground.Visible = false
end
for Map,ItemTable in pairs(ItemsInfo) do
for Index,Value in pairs(ItemTable) do
if Index == Item.OrginalName.Value then
Gui.InfoFrame.ItemName.Text = Value.Name
Gui.InfoFrame.Cost.Text = "Cost:" .. Value.Cost .. "$"
Gui.InfoFrame.BoostAddFrame.BoostAddInfo.Info.Text = Value.Boost1
end
end
end
end
function BuyItem()
local Children = ShopVar.Items:GetChildren()
table.sort(Children, function(a,b)
return a.Name < b.Name
end)
local Item = Children[OnSlot]
for Map,ItemTable in pairs(ItemsInfo) do
for Index,Value in pairs(ItemTable) do
if Index == Item.OrginalName.Value then
if plr.leaderstats.Money.Value >= Value.Cost then
Remotes.BuyItem:FireServer(Value.Cost, Item)
Item.Unlocked.Value = true
SetItemInfo()
end
end
end
end
end
function EquipItem()
local Children = ShopVar.Items:GetChildren()
for i,v in pairs(Children) do
if v.Equiped.Value then
v.Equiped.Value = false
end
end
table.sort(Children, function(a,b)
return a.Name < b.Name
end)
local Item = Children[OnSlot]
for Map,ItemTable in pairs(ItemsInfo) do
for Index,Value in pairs(ItemTable) do
if Index == Item.OrginalName.Value then
Remotes.EquipItem:FireServer(Item)
Item.Equiped.Value = true
SetItemInfo()
end
end
end
end
function UnEquipItem()
local Children = ShopVar.Items:GetChildren()
table.sort(Children, function(a,b)
return a.Name < b.Name
end)
local Item = Children[OnSlot]
for Map,ItemTable in pairs(ItemsInfo) do
for Index,Value in pairs(ItemTable) do
if Index == Item.OrginalName.Value then
Remotes.UnEquipItem:FireServer(Item)
Item.Equiped.Value = false
SetItemInfo()
end
end
end
end
function NextCamera()
OnSlot = math.min(OnSlot + 1 , #CamsVar)
Camera.CFrame = CamsVar[OnSlot].CFrame
SetItemInfo()
end
function PrevCamera()
OnSlot = math.max(OnSlot - 1 , 1)
Camera.CFrame = CamsVar[OnSlot].CFrame
SetItemInfo()
end
Gui.NextItem.MouseButton1Click:Connect(NextCamera)
Gui.PrevItem.MouseButton1Click:Connect(PrevCamera)
Gui.BuyBackground.Buy.MouseButton1Click:Connect(BuyItem)
Gui.EquipBackground.Equip.MouseButton1Click:Connect(EquipItem)
Gui.UnEquipBackground.UnEquip.MouseButton1Click:Connect(UnEquipItem)
Remotes.OpenShop.OnClientEvent:Connect(OpenShop)
Remotes.CloseShop.OnClientEvent:Connect(CloseShop)
and sory for my english i use translator