My script does not work after the player re-enters

Hello everyone I created a script for buying a fushion and when I buy the next fushion and exit, I go in and click buy, the previous fushions are bought from me, or rather the initial fushions, and I don’t understand What’s the matter? The local script is responsible for purchasing the fusion, and the server script is responsible for providing the fusion.
local script

local player = game.Players.LocalPlayer
local totalPower = player:WaitForChild("TotalPower")
local fusion = player.leaderstats:WaitForChild("Fusion")

local buyFusion = game.ReplicatedStorage:WaitForChild("FusionUp") -- Event for buying fusions

local AbbreviateModule = require(game.ReplicatedStorage:WaitForChild("Abbreviate")) -- Load abbreviation module

local acceptedFusions = {
    "None", "Werewolf", "Minotaur", "Gryphon", 
    "Phoenix", "Yeti", "Hydra", "Reaper"
}
local fusionBoosts = {
    None = 0, 
    Werewolf = 25, 
    Minotaur = 500, 
    Gryphon = 10000, 
    Phoenix = 250000, 
    Yeti = 100000000,
    Hydra = 100000000,
    Reaper = 2000000000
}
local fusionPrices = {
    None = 500, 
    Werewolf = 5e21, 
    Minotaur = 1.99e27, 
    Gryphon = 3e32, 
    Phoenix = 7e36, 
    Yeti = 5e41, 
    Hydra = 2e49,
    Reaper = 15e51 
}

-- Define required classes for each fusion card
local requiredClasses = {
    None = nil, 
    Werewolf = "S-Class", 
    Minotaur = "SS-Class", 
    Gryphon = "SSS-Class", 
    Phoenix = "X-Class",
    Yeti = "Y-Class",
    Hydra = "Z-Class",
    Reaper = "XYZ-Class"
}

-- Class hierarchy definition
local classHierarchy = { 
    "None", "F-Class", "E-Class", "D-Class", "C-Class", "B-Class",
    "A-Class", "S-Class", "SS-Class", "SSS-Class", "X-Class", 
    "Y-Class", "Z-Class", "XYZ-Class"
}

-- Function to compare classes
local function hasRequiredClass(playerClass, requiredClass)
    local playerClassIndex = table.find(classHierarchy, playerClass)
    local requiredClassIndex = table.find(classHierarchy, requiredClass)

    return playerClassIndex and requiredClassIndex and playerClassIndex >= requiredClassIndex
end

-- UI elements
local priceText = script.Parent.Parent.PriceText
local fusionNameText = script.Parent.Parent.ClassNameText
local yourBoostText = script.Parent.Parent.YourBoost
local nextFusionText = script.Parent.Parent.NextClassName2
local nextBoostText = script.Parent.Parent.NextBoostText
local classText = script.Parent.Parent.Cost_Title -- Assuming this is the class title text field
local imageFusion = script.Parent.Parent.ImageFusion

-- Dictionary of fusion images
local fusionImages = {
    None = "rbxassetid://12345678", -- Replace with real image IDs
    Werewolf = "rbxassetid://101327226569472",
    Minotaur = "rbxassetid://87286241154287",
    Gryphon = "rbxassetid://135121206180771",
    Phoenix = "rbxassetid://116869246657778",
    Yeti = "rbxassetid://75671867972101",
    Hydra = "rbxassetid://86234585407677",
    Reaper = "rbxassetid://78596750479540"
}

-- Debounce to prevent spamming
local debounce = false

-- Function to update the fusion display
local function updateFusionDisplay()
    local currentIndex = table.find(acceptedFusions, fusion.Value) or 0
    local selectedFusion = acceptedFusions[currentIndex + 1] or "MAX"

    fusionNameText.Text = fusion.Value
    yourBoostText.Text = "Boost: " .. AbbreviateModule.abbreviate(fusionBoosts[fusion.Value] or 0) -- Abbreviate Boost
    nextFusionText.Text = selectedFusion

    local nextPrice = fusionPrices[selectedFusion]
    if nextPrice then
        priceText.Text = "Price: " .. AbbreviateModule.abbreviate(nextPrice) .. " 💪" -- Abbreviate price
        script.Parent.Visible = true
    else
        priceText.Text = "MAX"
        script.Parent.Visible = false
    end

    nextBoostText.Text = "Next Boost: " .. AbbreviateModule.abbreviate(fusionBoosts[selectedFusion] or "MAX") -- Abbreviate Boost

    -- Update the required class text for the next fusion
    local requiredClass = requiredClasses[selectedFusion]
    classText.Text = requiredClass and " " .. requiredClass or "No Class Required"

    -- Update the current fusion image
    if fusion.Value == "None" then
        imageFusion.Visible = false -- Hide image if fusion is "None"
    else
        imageFusion.Image = fusionImages[fusion.Value] -- Set image for current fusion
        imageFusion.Visible = true -- Show image
    end
end

-- Function to handle fusion purchase
script.Parent.MouseButton1Click:Connect(function()
    if debounce then return end
    debounce = true

    local currentIndex = table.find(acceptedFusions, fusion.Value) or 0
    local selectedFusion = acceptedFusions[currentIndex + 1]
    local requiredPrice = fusionPrices[selectedFusion]
    local requiredClass = requiredClasses[selectedFusion]

    -- Check if enough Total Power and if the required class is met
    if totalPower.Value >= requiredPrice then
        -- Check if player has the required class or is above it
        if requiredClass == nil or hasRequiredClass(player.leaderstats.Class.Value, requiredClass) then
            buyFusion:FireServer(selectedFusion) -- Send event to server for purchase
            totalPower.Value -= requiredPrice -- Decrease Total Power
            fusion.Value = selectedFusion -- Update current fusion
            updateFusionDisplay() -- Update UI

            -- Move player to spawn
            local spawnLocation = workspace:FindFirstChild("SpawnLocation") -- Ensure you have a SpawnLocation object
            if spawnLocation then
                player.Character:MoveTo(spawnLocation.Position) -- Move player
            else
                warn("SpawnLocation not found.")
            end
        else
            print("Required class for purchase: " .. requiredClass)
        end
    else
        print("Not enough total power to buy fusion.")
    end

    wait(0.1) -- Pause before removing debounce
    debounce = false
end)

-- Initialize display on startup
updateFusionDisplay()

-- Subscribe to fusion changes
fusion.Changed:Connect(updateFusionDisplay)

server scipt

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local event = RS.ServerMessage  -- Make sure this event exists in ReplicatedStorage
local ranks = require(game:GetService("ServerStorage").FusionsModule)

local function buy(player)
	local tp = player:FindFirstChild("TotalPower") -- Make sure TotalPower exists
	if not tp then
		print("TotalPower not found.")
		return
	end

	-- Collect all owned ranks
	local ownedRanks = {}
	for _, rank in pairs(ranks) do
		if rank["Owned"] then
			table.insert(ownedRanks, rank)
		end
	end

	-- Check if there are owned fusions
	if #ownedRanks == 0 then
		print("You do not own any fusions.")
		return
	end

	-- Get the best fusion
	local bestRank = ownedRanks[#ownedRanks]
	local nextRank = ranks["Fusion" .. (bestRank.rank + 1)]

	if nextRank then
		if tp.Value >= nextRank.Price then
			-- Reset player values
			player.Strength.Value = 0
			player.Psp.Value = 0
			player.Endurance.Value = 0
			player.Agility.Value = 0

			-- Set the class
			if player.leaderstats then
				player.leaderstats.Class.Value = "F-Class"
			end

			-- Update the best fusion
			player.leaderstats.Fusion.Value = nextRank.Name
			player.BestFusion.FusionMulti.Value = nextRank.Multi

			-- Mark the next fusion as owned
			nextRank.Owned = true

			-- Reset ownership of previous fusions
			for i, rank in pairs(ranks) do
				if rank.rank <= bestRank.rank then
					rank.Owned = false
				end
			end

			-- Deduct Total Power from the player by the fusion's price
			tp.Value = tp.Value - nextRank.Price

			-- Send a message about the fusion purchase
			event:FireAllClients(player.Name .. " Buy Fusion: " .. nextRank.Name)

			print("You successfully purchased the fusion: " .. nextRank.Name)
		else
			print("Not enough Total Power to buy the next fusion.")
		end
	else
		print("You have reached the maximum fusion level.")
	end
end

RS.FusionUp.OnServerEvent:Connect(buy)

Any help is appreciated and it’s still relevant.

Still relevant help me decide I don’t know how to fix it

I realized that there is an error in this script, but I don’t know how to fix it, please help.

This is relevant, please help me decide, good people

Hey, I’m willing to help! Investigating as of right now

omg bro I’m so glad in tears the only one on the forum who can help this problem thank you I’m crying out of joy

1 Like

Hey! So there’s an issue which is a “begginer” error where you are trying to modify a player Value in a Localscript. Keep in mind that everything you change something in a localscript (which is what you are trying to do), it’ll not change for the server. Let’s say you have a Coins value, if you are trying to modify the value in a localscript, that’s an error you shouldn’t do. What I’d recommend doing in these cases is when you’re trying to change anything on the player, make a remote event (with checks), pass the params, get them in a server-sided script and then modify them.

I’ll try and modify your script now!

For the LocalScript,

Remove the following lines:

            totalPower.Value -= requiredPrice -- Decrease Total Power
            fusion.Value = selectedFusion -- Update current fusion

I’m not sure how your script logic works so I’d just place those 2 lines wherever you want them to be executed in the server-sided script

Also,

                player.Character:MoveTo(spawnLocation.Position) -- Move player

Should be executed in the server (as you are trying to move the player locally so anyone that isn’t the player itself will see him bugged

Is it fixed? If it is, can you mark the solution on the reply that helped for the other people that are coming here?

No, it didn’t help. When I buy a fusion, I log in again and the initial fusion is bought again.

So you would have to buy the first fusion all over again?

Let me send you a modular script for more clarity

local fusions = {}

fusions.Fusion1 = {Name = "None", Price = 0, Multi = 1, Owned = true, rank = 1}
fusions.Fusion2 = {Name = "Werewolf", Price = 5e21, Multi = 25, Owned = false, rank = 2}
fusions.Fusion3 = {Name = "Minotaur", Price = 1.99e27, Multi = 500, Owned = false, rank = 3}
fusions.Fusion4 = {Name = "Gryphon", Price = 3e32, Multi = 10000, Owned = false, rank = 4}
fusions.Fusion5 = {Name = "Phoenix", Price = 7e36, Multi = 250000, Owned = false, rank = 5}
fusions.Fusion6 = {Name = "Yeti", Price = 5e41, Multi = 5000000, Owned = false, rank = 6}
fusions.Fusion7 = {Name = "Hydra", Price = 2e49, Multi = 100000000, Owned = false, rank = 7}
fusions.Fusion8 = {Name = "Reaper", Price = 15e51, Multi = 2000000000, Owned = false, rank = 8}

return fusions

So, would you have to buy the first fusion all over again?

Look, for example, I have a minotaur and I buy a fusion and I get a griffin and when I re-enter the game and click buy fusion, I get a werewolf instead of a griffin

1 Like

No, I want to make it so that the script can see what the player has and he can buy the next fusion and not the previous one

But it’s not like that for me now, I buy the initial Fusion and Another fushion, that is, the minotaur