My script is supposed to assign tycoons to players and then spawn the tycoon on a assigned position after pressing a button. The other button should unassign the tycoon and delete the tycoon from workspace. Problem is when the player spams both buttons very quickly, the leaderstats show that player has 5 tycoons and cant spawn them any more (player can have up to 5 tycoons), but only 3 tycoons are actually showing. Tried implementing a cooldown to prevent both functions running at the same time, but the glitch still occurs
local ws = game:GetService("Workspace")
local PPS = game:GetService("ProximityPromptService")
local SS = game:GetService("ServerStorage")
local PP1 = ws.PC.Keyboard.ProximityPrompt
local PP2 = ws.PC.Mouse.ProximityPrompt
PPS.MaxPromptsVisible = 2
local function onPlayerAdded(player)
local folder = Instance.new("Folder")
folder.Parent = ws
folder.Name = player.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local tycoons = Instance.new("IntValue")
tycoons.Name = "Tycoons"
tycoons.Parent = leaderstats
tycoons = 0
end
local coolDown = false
local function onProximityPrompt2(player)
if coolDown then return end
coolDown = true
local userId = player.UserId
local tycoon = ws:WaitForChild(userId)
local tycoons = SS:WaitForChild("Tycoons")
local museums = tycoon:GetChildren()
if #museums >= 1 then -- Check if the museums player owns are >= 1
player.leaderstats.Tycoons.Value -= 1 -- Reduce leaderstats
museums[1]:Destroy() --Destroy the museum
for i,v in tycoons:GetChildren() do
if v:GetAttribute("UserId") == userId then --Check which museum player owns
v:SetAttribute("Taken",false) --Remove ownership
v:SetAttribute("UserId",0)
break
end
end
end
coolDown = false
end
local function onProximityPrompt1(player)
if coolDown then return end
coolDown = true
local tycoons = SS:WaitForChild("Tycoons")
local userId = player.UserId
local tycoon = ws:WaitForChild(userId)
local museum = SS.Museum:Clone()
local position = false
for i, v in tycoons:GetChildren() do
local taken = v:GetAttribute("Taken")
print("Is museum taken: "..tostring(taken))
if taken ~= true then
v:SetAttribute("Taken",true)
v:SetAttribute("UserId",userId)
position = v.Position
player.leaderstats.Tycoons.Value += 1
break
end
end
if position then
museum:PivotTo(CFrame.new(position))
museum.Parent = tycoon
else
print("No available tycoons")
end
coolDown = false
end
--Events
game.Players.PlayerAdded:Connect(onPlayerAdded)
PP1.Triggered:Connect(onProximityPrompt1)
PP2.Triggered:Connect(onProximityPrompt2)