Code works as intended only when debugging

I have a value(WeaponName) that is passed from the client to the server through a RemoteEvent, that is stored in a table using a modulescript in the server, but this value isnt set through when testing the game normaly in the studio and no errors related to this shows, and when I start debugging the game to see whats hapenning it works

Client(LocalScript):

local CoreEvent:RemoteEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("CoreEvent")

for _, v in pairs(WeaponSelectorGUI.DropdownButton.DropdownFrame:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			CoreEvent:FireServer("WeaponSelect", v.Text)
		end)
	end
end

Server:

  • CoreScript:
local WeaponName = Arg1

plr:LoadCharacter()

char = plr.Character

for _, v in pairs(workspace:GetChildren()) do
	if v:IsA("SpawnLocation") then
			if v.Name == "SpawnLocation" then
			char:MoveTo(v.Position)
		end
	end
end
		
for _, v in pairs(WeaponFolder:GetChildren()) do
	if v.Name == WeaponName and not v:FindFirstChild("GamepassId") then
		char = plr.Character
		task.spawn(function()
			StatusScript.setCurrentWeapon(char, WeaponName)
		end)
	end
       if v.Name == WeaponName and v:FindFirstChild("GamepassId") and MPS:UserOwnsGamePassAsync(plr.UserId, v.GamePassId.Value) then
	char = plr.Character
		task.spawn(function()
			StatusScript.setCurrentWeapon(char, WeaponName)
              end)
	end
end

StatusScript:

function module.setCurrentWeapon(character, value)
        local data = StatusData.CharacterData[character]
        if data then
                data.CurrentWeapon = value
                local player = Players:GetPlayerFromCharacter(character)
                if player then
                        StatusData.UpdatePersistentData(player, "CurrentWeapon", value)
                end
        end
end

StatusData:

-- Create a table to store all character data
StatusData.CharacterData = {}

-- Create a table to store persistent player data
StatusData.PlayerData = {}

-- Function to initialize character data 
function StatusData.InitializeCharacter(character)
    local player = Players:GetPlayerFromCharacter(character)
    if not player then return end

    -- Check if theres existing player data
    local playerData = StatusData.PlayerData[player.UserId]
    
    StatusData.CharacterData[character] = {
        LastAttack = 0,
        CurrentWeapon = playerData and playerData.CurrentWeapon or "Fists",
        CurrentClass = playerData and playerData.CurrentClass or "Adventurer",
        ActualCombo = 1,
        PlayerPosture = 20,
        PrimaryState = "Idle",
        BooleanStates = {
            Stunned = false,
            Rooted = false,
            Parrying = false,
            Parried = false,
            Running = false,
            Blocking = false,
            Dodging = false,
            HyperArmor = false,
            Ragdolled = false
        }
    }
    
    -- Initialize persistent player data if it doesn't exist
    if not playerData then
        StatusData.PlayerData[player.UserId] = {
            CurrentWeapon = StatusData.CharacterData[character].CurrentWeapon,
            CurrentClass = StatusData.CharacterData[character].CurrentClass
        }
    end
end

those are the parts that manage that value in the scripts.

this is video proof of this happening

Im really lost, because other values set through the same script (StatusScript) are actually updated on the table.

2 Likes

Couple questions

  1. Do you know where the code is going wrong and are there any errors?

  2. Is there a reason you’re using a for loop to fire the server? Couldn’t you just use the mousebutton1click function

It is very confusing that your code is only working when debugging, but what do you mean by this? When you add print statements the code works or what?

1 Like
  1. Tbh I dont know where its going wrong if its going wrong at all, there is no errors associated with the code that manipules that value.

  2. I made that to check if the players has the gamepasses, but it should not give any problem, and I tested removing these loops but the value still isnt changing.

I didnt put print statements, I will try that.

The fact that it works when you run code line by line makes me think its somehow related to how functions run deferred now. But your code doesnt seem to have any problem with that, do you think its your stats gui code thats somehow messing it up? (the one you connect with in the vid to show status data)

mmm testing with print statements I figured how this happened, in the status script I have the function for setting the value of the weapon and I added print statements.

function module.setCurrentWeapon(character, value)
    print("Value: "..value)

    local data = StatusData.CharacterData[character]

    if data then
        print("Got data")
    else
        print("Nil data")
    end
    

    if data then
        data.CurrentWeapon = value
        local player = Players:GetPlayerFromCharacter(character)
        if player then
            StatusData.UpdatePersistentData(player, "CurrentWeapon", value)
        end
    end
end

and when I run the game normally the data variable is always nil, but when im debugging it can get the data.

Blue = Normal game run
Red = Debugging

Ignore the “Dash effect added” lol.

I still dont know why or how this happens, but im going to try by adding a function to get the data inestead of directly calling the table to see if this fixes it.

try to print out the character data table in status data on both occasions like this, that will clarify alot of things

I found that using the character name (Character.Name) inestead of using the character object for the table makes it works.

normal game run:
image
image

-- Function to initialize character data 
function StatusData.InitializeCharacter(character)
    local player = Players:GetPlayerFromCharacter(character)
    if not player then return end

    -- Check if theres existing player data
    local playerData = StatusData.PlayerData[player.UserId]
    
    StatusData.CharacterData[character.Name] = {
        LastAttack = 0,
        CurrentWeapon = playerData and playerData.CurrentWeapon or "Fists",
        CurrentClass = playerData and playerData.CurrentClass or "Adventurer",
        ActualCombo = 1,
        PlayerPosture = 20,
        PrimaryState = "Idle",
        BooleanStates = {
            Stunned = false,
            Rooted = false,
            Parrying = false,
            Parried = false,
            Running = false,
            Blocking = false,
            Dodging = false,
            HyperArmor = false,
            Ragdolled = false
        }
    }
    
    -- Initialize persistent player data if it doesn't exist
    if not playerData then
        StatusData.PlayerData[player.UserId] = {
            CurrentWeapon = StatusData.CharacterData[character.Name].CurrentWeapon,
            CurrentClass = StatusData.CharacterData[character.Name].CurrentClass
        }
    end
end
function module.setCurrentWeapon(character, value)
    print("Value: "..value)

    local data = StatusData.CharacterData[character.Name]
    
    if data then
        print("Got data")
    else
        print("Nil data")
    end
    

    if data then
        data.CurrentWeapon = value
        local player = Players:GetPlayerFromCharacter(character)
        if player then
            StatusData.UpdatePersistentData(player, "CurrentWeapon", value)
        end
    end
end

so indexing by the models name instead of the model itself fixed the problem, that is really strange. im glad you fixed it though (also this might be a bug, i see no reason it should act like this. you might want to report this)

ok, thanks for the help tho. I will report it to see if this effectively is a bug.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.