Hello everyone I got a small issue so here u can roll a mutation like Chocolate, Honey, Ice and Rainbow and when u leave it does save but sometimes when u leave longer it removes the mutation?
-- Tool scaling and inventory handling script with effects support and mutation saving (FIXED SCALE ROUNDING)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local destroyMexoEvent = ReplicatedStorage:WaitForChild("DestroyMexo")
local inventoryStore = DataStoreService:GetDataStore("InventoryStore")
local rewardFolder = ServerStorage:WaitForChild("Saved")
local mutationsFolder = ServerStorage:WaitForChild("Mutations")
local inventoryEvent = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("InventoryEvent")
local caughtMexoEvent = ReplicatedStorage:WaitForChild("CaughtMexoEvent")
local mexoToolMap = {
Mexo = "Basic Mexo",
Mexo1 = "Godly Mexo",
Mexo2 = "Uncommon Mexo",
Mexo3 = "Epic Mexo",
Mexo4 = "Legendary Mexo",
Mexo5 = "Rare Mexo",
Basic = "Basic Mexo",
Godly = "Godly Mexo",
Uncommon = "Uncommon Mexo",
Epic = "Epic Mexo",
Legendary = "Legendary Mexo",
Rare = "Rare Mexo"
}
-- Function to check if a tool is a Mexo tool
local function isMexoTool(toolName)
return toolName:find("Mexo") ~= nil
end
-- Function to round scale to 1 decimal place
local function roundScale(scale)
return math.floor(scale * 10 + 0.5) / 10
end
-- Function to get the next available counter for non-Mexo tools
local function getNextCounter(inventory, baseName)
local counter = 1
while inventory:FindFirstChild(baseName .. "_" .. counter) do
counter = counter + 1
end
return counter
end
local function storeOriginalHandleSize(tool)
local handle = tool:FindFirstChild("Handle")
if handle and handle:IsA("BasePart") then
tool:SetAttribute("OriginalHandleSizeX", handle.Size.X)
tool:SetAttribute("OriginalHandleSizeY", handle.Size.Y)
tool:SetAttribute("OriginalHandleSizeZ", handle.Size.Z)
end
end
local function scaleHandle(tool, scale)
if not tool then return end
local handle = tool:FindFirstChild("Handle")
if handle and handle:IsA("BasePart") then
local origX = tool:GetAttribute("OriginalHandleSizeX") or handle.Size.X
local origY = tool:GetAttribute("OriginalHandleSizeY") or handle.Size.Y
local origZ = tool:GetAttribute("OriginalHandleSizeZ") or handle.Size.Z
handle.Size = Vector3.new(origX, origY, origZ) * scale
handle.CanCollide = false
handle.Massless = true
handle.Anchored = false
if tool:GetAttribute("Neon") == true then
handle.Material = Enum.Material.Neon
end
end
end
-- Function to check if tool already has a specific mutation
local function hasExistingMutation(tool, mutationType)
-- Check for mutation flag
local mutationFlag = tool:FindFirstChild(mutationType .. "MutationFlag")
if mutationFlag then
return true
end
-- Check for mutation part
local mutationPart = tool:FindFirstChild(mutationType .. "Mutation")
if mutationPart then
return true
end
return false
end
-- FIXED: Function to add mutation to tool with proper scaling
local function addMutationToTool(tool, mutationType)
print("=== ADDING MUTATION TO TOOL ===")
print("Tool Name:", tool.Name)
print("Mutation Type:", mutationType)
-- Check if mutation already exists
if hasExistingMutation(tool, mutationType) then
print("β οΈ Mutation", mutationType, "already exists on tool:", tool.Name)
return true -- Return true since the mutation is already there
end
-- Get the mutation part from the mutations folder
local mutationPart = mutationsFolder:FindFirstChild(mutationType)
if not mutationPart then
warn("Mutation part not found:", mutationType)
return false
end
-- Make sure it's a BasePart
if not mutationPart:IsA("BasePart") then
warn("Mutation is not a BasePart:", mutationPart.ClassName)
return false
end
-- Get the tool's handle
local handle = tool:FindFirstChild("Handle")
if not handle or not handle:IsA("BasePart") then
warn("Tool handle not found or not a BasePart")
return false
end
-- Clone the mutation part
local clonedMutation = mutationPart:Clone()
clonedMutation.Name = mutationType .. "Mutation"
-- Get the tool's current scale
local toolScale = tool:GetAttribute("Scale") or 1
-- Calculate the mutation size considering the tool's scale
local baseHandleSize
local origX = tool:GetAttribute("OriginalHandleSizeX")
local origY = tool:GetAttribute("OriginalHandleSizeY")
local origZ = tool:GetAttribute("OriginalHandleSizeZ")
if origX and origY and origZ then
-- Use original size scaled by the tool's scale
baseHandleSize = Vector3.new(origX, origY, origZ) * toolScale
else
-- Fallback to current handle size
baseHandleSize = handle.Size
end
-- Set the mutation part properties (1.01x size of the scaled handle)
clonedMutation.Size = baseHandleSize * 1.01
clonedMutation.CFrame = handle.CFrame
clonedMutation.CanCollide = false
clonedMutation.Massless = true
clonedMutation.Anchored = false
-- Create a WeldConstraint to attach the mutation to the handle
local weld = Instance.new("WeldConstraint")
weld.Part0 = handle
weld.Part1 = clonedMutation
weld.Parent = handle
-- Parent the mutation to the tool
clonedMutation.Parent = tool
-- Create a BoolValue to track the mutation
local mutationValue = Instance.new("BoolValue")
mutationValue.Name = mutationType .. "MutationFlag"
mutationValue.Value = true
mutationValue.Parent = tool
print("Successfully added", mutationType, "mutation to tool with scale", toolScale)
return true
end
-- Function to rescale all mutations on a tool after handle scaling
local function rescaleMutations(tool)
local handle = tool:FindFirstChild("Handle")
if not handle or not handle:IsA("BasePart") then
return
end
-- Look for mutation parts
for _, child in ipairs(tool:GetChildren()) do
if child:IsA("BasePart") and child.Name:match("(.+)Mutation$") then
-- This is a mutation part, resize it to be 1.01x the current handle size
child.Size = handle.Size * 1.01
print("π§ Rescaled mutation:", child.Name, "to size:", child.Size)
end
end
end
-- Function to detect mutations on a tool
local function detectMutations(tool)
local mutations = {}
-- Check for mutation flags
for _, child in ipairs(tool:GetChildren()) do
if child:IsA("BoolValue") and child.Name:match("(.+)MutationFlag$") then
local mutationType = child.Name:match("(.+)MutationFlag$")
mutations[mutationType] = true
end
end
return mutations
end
-- Function to apply particle effects to handle
local function applyParticleEffect(handle, effectName)
local effectTemplate = rewardFolder:FindFirstChild(effectName)
if effectTemplate and effectTemplate:IsA("ParticleEmitter") then
local effectEmitter = effectTemplate:Clone()
effectEmitter.EmissionDirection = Enum.NormalId.Front
effectEmitter.Parent = handle
print("β¨ Added forward-facing", effectName, "effect to tool")
return true
else
warn("β No", effectName, "ParticleEmitter found in ServerStorage.Saved")
return false
end
end
-- Function to apply all visual effects to a tool
local function applyVisualEffects(tool)
local handle = tool:FindFirstChild("Handle")
if not handle then return end
local isNeon = tool:GetAttribute("Neon") or false
local hasGlow = tool:GetAttribute("HasGlow") or false
local hasGold = tool:GetAttribute("HasGold") or false
local hasStar = tool:GetAttribute("HasStar") or false
local hasHeart = tool:GetAttribute("HasHeart") or false
local hasDizzy = tool:GetAttribute("HasDizzy") or false
-- Apply neon material
if isNeon then
handle.Material = Enum.Material.Neon
end
-- Apply glow effect
if hasGlow then
local light = Instance.new("PointLight")
light.Name = "WeatherGlow"
light.Color = Color3.new(0.8, 0.9, 1)
light.Brightness = 5
light.Range = 20
light.Parent = handle
handle.Material = Enum.Material.Neon
handle.Color = Color3.new(0.6, 0.95, 1)
handle.UsePartColor = true
end
-- Apply gold effect
if hasGold then
handle.Color = Color3.new(1, 0.84, 0)
handle.Material = Enum.Material.Neon
handle.UsePartColor = true
local goldValue = Instance.new("BoolValue")
goldValue.Name = "Gold"
goldValue.Value = true
goldValue.Parent = handle
end
-- Apply particle effects
if hasStar and not handle:FindFirstChild("Star") then
applyParticleEffect(handle, "Star")
end
if hasHeart and not handle:FindFirstChild("Heart") then
applyParticleEffect(handle, "Heart")
end
if hasDizzy and not handle:FindFirstChild("Dizzy") then
applyParticleEffect(handle, "Dizzy")
end
end
-- Function to create a complete tool with all effects and mutations
local function createCompleteTool(toolTemplate, attributes, mutations)
local clonedTool = toolTemplate:Clone()
-- Store original handle size first
storeOriginalHandleSize(clonedTool)
-- Set all attributes
for attributeName, attributeValue in pairs(attributes) do
clonedTool:SetAttribute(attributeName, attributeValue)
end
-- Apply visual effects
applyVisualEffects(clonedTool)
-- Apply mutations BEFORE scaling (they will be sized correctly)
if mutations and next(mutations) then
for mutationType, hasMutation in pairs(mutations) do
if hasMutation then
local success = addMutationToTool(clonedTool, mutationType)
if success then
print("β
Applied", mutationType, "mutation to tool:", clonedTool.Name)
else
warn("β Failed to apply", mutationType, "mutation to tool:", clonedTool.Name)
end
end
end
end
-- Scale the handle and mutations together
local scale = attributes.Scale or 1
scaleHandle(clonedTool, scale)
rescaleMutations(clonedTool) -- Ensure mutations stay 1.01x handle size
return clonedTool
end
destroyMexoEvent.OnServerEvent:Connect(function(player, model)
if not (model and model:IsA("Model") and model.Name:match("^Mexo[0-5]?$")) then return end
local toolName = mexoToolMap[model.Name]
if not toolName then return end
local toolTemplate = rewardFolder:FindFirstChild(toolName)
if not toolTemplate then
warn("Tool not found in ServerStorage.Saved: " .. toolName)
return
end
-- Calculate scale ONCE and store it
local mexoScale = model:GetAttribute("MexoScale")
local scale = 1
if mexoScale then
scale = mexoScale
else
if model.PrimaryPart then
local currentSize = model.PrimaryPart.Size
local templateHandle = toolTemplate:FindFirstChild("Handle")
if templateHandle then
local scaleX = currentSize.X / templateHandle.Size.X
local scaleY = currentSize.Y / templateHandle.Size.Y
local scaleZ = currentSize.Z / templateHandle.Size.Z
scale = (scaleX + scaleY + scaleZ) / 3
end
end
end
-- Round the scale to 1 decimal place
scale = roundScale(scale)
print("π― Rounded scale:", scale)
-- Rest of the detection code stays the same...
local isNeon = false
for _, part in ipairs(model:GetDescendants()) do
if part:IsA("BasePart") and part.Material == Enum.Material.Neon then
isNeon = true
break
end
end
caughtMexoEvent:FireClient(player, model.Name, isNeon)
local hasGlow = model:GetAttribute("HasGlow") or false
local hasGold = model:GetAttribute("HasGold") or false
local hasStar = model:GetAttribute("HasStar") or false
local hasHeart = model:GetAttribute("HasHeart") or false
local hasDizzy = model:GetAttribute("HasDizzy") or false
local humanoidRootPart = model:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
if not hasStar and humanoidRootPart:FindFirstChild("Star") then
hasStar = true
end
if not hasHeart and humanoidRootPart:FindFirstChild("Heart") then
hasHeart = true
end
if not hasDizzy and humanoidRootPart:FindFirstChild("Dizzy") then
hasDizzy = true
end
end
model:Destroy()
local inventory = player:FindFirstChild("Inventory")
if not inventory then
inventory = Instance.new("Folder")
inventory.Name = "Inventory"
inventory.Parent = player
end
-- Use the rounded scale
local attributes = {
Scale = scale, -- This is the rounded scale
Neon = isNeon,
HasGlow = hasGlow,
HasGold = hasGold,
HasStar = hasStar,
HasHeart = hasHeart,
HasDizzy = hasDizzy
}
local clonedTool = createCompleteTool(toolTemplate, attributes)
clonedTool.Name = toolName
clonedTool.Parent = inventory
print("π― Tool created with rounded scale:", scale)
end)
Players.PlayerAdded:Connect(function(player)
local inventory = Instance.new("Folder")
inventory.Name = "Inventory"
inventory.Parent = player
-- Wait a bit to ensure the client is ready
wait(1)
local success, data = pcall(function()
return inventoryStore:GetAsync("Inventory_" .. player.UserId)
end)
if success and data then
for _, itemData in ipairs(data) do
local baseName = itemData.name
local scale = roundScale(itemData.scale or 1) -- Round scale when loading
local isNeon = itemData.neon or false
local hasGlow = itemData.hasGlow or false
local hasGold = itemData.hasGold or false
local hasStar = itemData.hasStar or false
local hasHeart = itemData.hasHeart or false
local hasDizzy = itemData.hasDizzy or false
local counter = itemData.counter
local mutations = itemData.mutations or {}
local savedTool = rewardFolder:FindFirstChild(baseName)
if savedTool then
-- Create display name based on tool type
local displayName
if isMexoTool(baseName) then
displayName = baseName .. "(" .. scale .. ")"
else
local toolCounter = counter or getNextCounter(inventory, baseName)
displayName = baseName .. "_" .. toolCounter
end
-- Create tool attributes
local attributes = {
Scale = scale, -- Use the rounded scale
Neon = isNeon,
HasGlow = hasGlow,
HasGold = hasGold,
HasStar = hasStar,
HasHeart = hasHeart,
HasDizzy = hasDizzy
}
-- Store counter for non-Mexo tools
if not isMexoTool(baseName) then
attributes.Counter = counter or getNextCounter(inventory, baseName)
end
-- Create the complete tool with all effects and mutations
local toolClone = createCompleteTool(savedTool, attributes, mutations)
toolClone.Name = displayName
toolClone.Parent = inventory
-- Fire the client event
print("π¦ Sending loaded item to client:", displayName)
inventoryEvent:FireClient(player, displayName, true)
else
warn("Missing saved tool: " .. baseName)
end
end
end
-- Handle new items added to inventory (for real-time catches)
inventory.ChildAdded:Connect(function(item)
-- Only handle items that don't already have the proper format
if not item.Name:match("(.+)%(.+%)$") and not item.Name:match("(.+)_(%d+)$") then
task.wait(0.1)
local baseName = item.Name
local displayName
if isMexoTool(baseName) then
-- Mexo tools use scale format with rounded scale
local scale = roundScale(item:GetAttribute("Scale") or 1)
item:SetAttribute("Scale", scale) -- Update the attribute with rounded scale
displayName = baseName .. "(" .. scale .. ")"
else
-- Non-Mexo tools use counter format
local counter = getNextCounter(inventory, baseName)
displayName = baseName .. "_" .. counter
item:SetAttribute("Counter", counter)
end
-- Rename the item
item.Name = displayName
print("π¦ Sending new item to client:", displayName)
inventoryEvent:FireClient(player, displayName, true)
end
end)
end)
Players.PlayerRemoving:Connect(function(player)
local inventory = player:FindFirstChild("Inventory")
if not inventory then return end
local saveData = {}
for _, tool in pairs(inventory:GetChildren()) do
local baseName, scale, counter
if isMexoTool(tool.Name) then
-- Extract base name from Mexo format: "Basic Mexo(1.5)" -> "Basic Mexo"
baseName = tool.Name:match("(.+)%(.+%)$") or tool.Name
scale = roundScale(tool:GetAttribute("Scale") or 1) -- Round scale when saving
else
-- Extract base name from counter format: "Basic Ball_2" -> "Basic Ball"
baseName = tool.Name:match("(.+)_(%d+)$") or tool.Name
scale = roundScale(tool:GetAttribute("Scale") or 1) -- Round scale when saving
counter = tool:GetAttribute("Counter")
end
local neon = tool:GetAttribute("Neon") or false
local hasGlow = tool:GetAttribute("HasGlow") or false
local hasGold = tool:GetAttribute("HasGold") or false
local hasStar = tool:GetAttribute("HasStar") or false
local hasHeart = tool:GetAttribute("HasHeart") or false
local hasDizzy = tool:GetAttribute("HasDizzy") or false
-- Detect and save mutations
local mutations = detectMutations(tool)
local saveEntry = {
name = baseName,
scale = scale, -- This will be the rounded scale
neon = neon,
hasGlow = hasGlow,
hasGold = hasGold,
hasStar = hasStar,
hasHeart = hasHeart,
hasDizzy = hasDizzy,
mutations = mutations,
}
-- Only save counter for non-Mexo tools
if not isMexoTool(baseName) and counter then
saveEntry.counter = counter
end
table.insert(saveData, saveEntry)
-- Debug print for mutations
if next(mutations) then
print("πΎ Saving mutations for", tool.Name, ":")
for mutationType, hasMutation in pairs(mutations) do
if hasMutation then
print(" -", mutationType)
end
end
end
end
local success, err = pcall(function()
inventoryStore:SetAsync("Inventory_" .. player.UserId, saveData)
end)
if not success then
warn("Failed to save inventory for " .. player.Name .. ": " .. tostring(err))
end
end)
inventoryEvent.OnServerEvent:Connect(function(player, itemName, value, button)
local inventory = player:FindFirstChild("Inventory")
if not inventory then return end
local selectedItem = inventory:FindFirstChild(itemName)
if not selectedItem then return end
local backpack = player:FindFirstChild("Backpack")
local character = player.Character
if not backpack or not character then return end
if value == false then
local equippedInBackpack = backpack:FindFirstChild(itemName)
local equippedInCharacter = character:FindFirstChild(itemName)
if not equippedInBackpack and not equippedInCharacter then
-- Get all attributes from the selected item
local attributes = {}
for _, attributeName in ipairs({"Scale", "Neon", "HasGlow", "HasGold", "HasStar", "HasHeart", "HasDizzy", "Counter"}) do
local attributeValue = selectedItem:GetAttribute(attributeName)
if attributeValue ~= nil then
-- Round scale attribute if it's the Scale attribute
if attributeName == "Scale" then
attributeValue = roundScale(attributeValue)
end
attributes[attributeName] = attributeValue
end
end
-- Get mutations
local mutations = detectMutations(selectedItem)
-- Create a fresh tool with all effects and mutations
local baseName = selectedItem.Name:match("(.+)%(.+%)$") or selectedItem.Name:match("(.+)_(%d+)$") or selectedItem.Name
local toolTemplate = rewardFolder:FindFirstChild(baseName)
if toolTemplate then
local toolClone = createCompleteTool(toolTemplate, attributes, mutations)
toolClone.Name = itemName
toolClone.Parent = backpack
end
if button then
button.Text = "Unequip"
button.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
end
else
if equippedInBackpack then equippedInBackpack:Destroy() end
if equippedInCharacter then equippedInCharacter:Destroy() end
if button then
button.Text = "Equip"
button.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
end
end
else
local equipped = backpack:FindFirstChild(itemName) or character:FindFirstChild(itemName)
if not equipped then
-- Get all attributes from the selected item
local attributes = {}
for _, attributeName in ipairs({"Scale", "Neon", "HasGlow", "HasGold", "HasStar", "HasHeart", "HasDizzy", "Counter"}) do
local attributeValue = selectedItem:GetAttribute(attributeName)
if attributeValue ~= nil then
-- Round scale attribute if it's the Scale attribute
if attributeName == "Scale" then
attributeValue = roundScale(attributeValue)
end
attributes[attributeName] = attributeValue
end
end
-- Get mutations
local mutations = detectMutations(selectedItem)
-- Create a fresh tool with all effects and mutations
local baseName = selectedItem.Name:match("(.+)%(.+%)$") or selectedItem.Name:match("(.+)_(%d+)$") or selectedItem.Name
local toolTemplate = rewardFolder:FindFirstChild(baseName)
if toolTemplate then
local toolClone = createCompleteTool(toolTemplate, attributes, mutations)
toolClone.Name = itemName
toolClone.Parent = backpack
end
if button then
button.Text = "Unequip"
button.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
end
end
end
end)