Hi there! I am making a shop gui script with datasaves and stuff. However I came across a problem where sometimes my script dont work and sometimes it does?? No changes has been made in the script from both videos
Any help would be appreciated
working clip: https://medal.tv/games/roblox-studio/clips/6Lt15e2oK60cc/d1337j0SdUkn?invite=cr-MSxPSmIsMjQ1MjQwOTcs
not working clip: https://medal.tv/games/roblox-studio/clips/6LMRewbmKi-0Y/d1337JOoXWUV?invite=cr-MSxKOGssMjQ1MjQwOTcs
TextButton script:
button.MouseButton1Click:Connect(function()
print("Clicked")
local itemname = button.Parent.Name
if button.Name == "Buy" then
local result = BuyTitleRemote:InvokeServer(itemname)
print(result)
if result == true then
button.Name = "Equip"
button.Text = "Equip"
elseif result == false then
button.Text = "Not enough tokens"
wait(1)
button.Text = button.Name
end
elseif button.Name == "Equip" then
print("Equipping")
local result = EquipTitleRemote:InvokeServer(itemname)
print(result)
if result == true then
local scrollFrame = button.Parent.Parent
for i,v in pairs(scrollFrame:GetChildren()) do
if v.Name == "UIGridLayout" then
else
local oldbutton = v:FindFirstChildWhichIsA("TextButton")
if result == not nil then
print(oldbutton)
if oldbutton.Name == "UnEquip" then
print("Done")
oldbutton.Name = "Equip"
oldbutton.Text = "Equip"
elseif result == nil then
end
end
end
end
button.Name = "Unequip"
button.Text = "Unequip"
elseif result == false then
button.Text = "Error!"
wait(1)
button.Text = "Equip"
end
elseif button.Name == "Unequip" then
local result = UnequipTitleRemote:InvokeServer(itemname)
if result == true then
button.Text = "Equip"
button.Name = "Equip"
else
button.Text = "Error!"
button.Text = "Unequip"
button.Name = "Unequip"
end
end
end)
module script:
local module = {
CheckExp = function(players)
for i,v in pairs(players) do
local playervalues = v.PlayerValues
local Exp = playervalues.exp
local ExpRequired = playervalues.Exprequired
local level = v.leaderstats.Level
if Exp.Value >= ExpRequired.Value then
level.Value = level.Value +1
local newExp = Exp.Value - ExpRequired.Value
Exp.Value = newExp
end
end
end,
BuyTitle = function(plrName,ItemName)
local player = game.Players:FindFirstChild(plrName)
local inventory = player.OwnedTitles
local Tokens = player.PlayerValues.Tokens.Value
local Titles = game.ReplicatedStorage.Titles
local Item = Titles:FindFirstChild(ItemName)
local Price = Item.Price.Value
if Tokens >= Price then
local endtokens = Tokens - Price
Tokens = endtokens
local insert = Instance.new("StringValue")
insert.Name = Item.Name
insert.Value = Item.Name
insert.Parent = inventory
print("true")
return true
else
print("false")
return false
end
end,
EquipTitle = function(plr,Character,ItemName)
local head = Character.Head
if plr.OwnedTitles == ItemName then
print(ItemName)
return false
else
for i,v in pairs(head:GetChildren()) do
if v:IsA("BillboardGui") then
v:Destroy()
else
print("it is not")
end
end
local Success,err = pcall(function()
local item = game.ReplicatedStorage.Titles:FindFirstChild(ItemName):Clone()
item.Parent = head
plr.EquippedTitle.Value = ItemName
print(ItemName)
end)
return true
end
end,
UnequipTitle = function(plr,Character,ItemName)
local head = Character.Head
local EquippedTitle = plr.EquippedTitle
if head:FindFirstChild(ItemName) == nil then
return false
else
head:FindFirstChild(ItemName):Destroy()
EquippedTitle.Value = ""
return true
end
end,
}
return module
server script:
wait(game.ServerScriptService.Leaderboard)
print("ok")
local Remote = game.ReplicatedStorage.Remotes
local CheckModule = require(game.ServerScriptService.Checkstuff)
local BuyTitle = Remote.BuyTitle
local EquipTitle = Remote.EquipTitle
local UnequipTitle = Remote.UnequipTitle
local plyrs = game:GetService("Players")
plyrs.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(Char)
BuyTitle.OnServerInvoke = function(player,ItemName)
local plyrname = player.Name
return CheckModule.BuyTitle(plyrname,ItemName)
end
EquipTitle.OnServerInvoke = function(player,ItemName)
return CheckModule.EquipTitle(player,Char,ItemName)
end
UnequipTitle.OnServerInvoke = function(player,ItemName)
return CheckModule.UnequipTitle(player,Char,ItemName)
end
end)
end)