Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[Solved]How come the player keeps getting the wrong tool?

Asked by 2 years ago
Edited 2 years ago

I'm currently working on a spawn system for my game and the tools are dependent on a string value, but for some reason even tho the string value does not equal the required string it keeps giving the player the wrong tool What Is the problem here. I don't even get errors just a blank output. (kinda I still get prints and stuff.)

Script

function onSpawnPlayer(Player)
    wait(.1)
    local MainPlayerGui = game.StarterGui["MainGui"]:Clone()
    if not Player.PlayerGui:FindFirstChild("MainGui") then
        MainPlayerGui.Parent = Player.PlayerGui
    else
        Player.PlayerGui:FindFirstChild("MainGui"):Destroy()
        MainPlayerGui.Parent = Player.PlayerGui
    end
    ReplicatedStorage.Events.SpawnEvent.OnServerEvent:Connect(function(Player) --Everthing beyond this line does not work as intended.
        local Country = Player.PlayerGui.MainGui.ValuesFolder.Country or ReplicatedStorage.PlayerCountries[Player.Name.."Country"]
        local Class = Player.PlayerGui.MainGui.ValuesFolder.Class or ReplicatedStorage.PlayerCountries.PlayerClasses[Player.Name.."Class"]
        MainPlayerGui:Destroy()
        Player:LoadCharacter()
        if Country.Value == "Britain" then
            if Class.Value == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Smle-MK3"]:Clone()
                RifleManTool.Parent = Player.Backpack
            elseif Class.Value == "Sapper" then
                local SapperTool = TeamTools.Primary["Lewis Gun"]:Clone()
                SapperTool.Parent = Player.Backpack
            end
        elseif Country.Value == "France" then
            if Class.Value == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Berthier-07"]:Clone()
                RifleManTool.Parent = Player.Backpack
            end
        elseif Country.Value == "America" then
            if Class.Value == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Springfield M1903"]:Clone()
                RifleManTool.Parent = Player.Backpack
            elseif Class.Value == "Sapper" then
                local SapperTool = TeamTools.Primary["Lewis Gun"]:Clone()
                SapperTool.Parent = Player.Backpack
            end
        end
        if Country.Value == "Germany" then
            if Class.Value == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Gewehr-98"]:Clone()
                RifleManTool.Parent = Player.Backpack
            end
        elseif Country.Value == "Austria-Hungary" then
            if Class.Value == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Gewehr-88"]:Clone()
                RifleManTool.Parent = Player.Backpack
            end
        elseif Country.Value == "Ottoman Empire" then
            if Class.Value == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Turkish Mauser"]:Clone()
                RifleManTool.Parent = Player.Backpack
            end
        end
    end)
    function onPlayerDeath(Character)
        print("Died")
        if Player.PlayerGui:FindFirstChild("StatusUI") then
            Player.PlayerGui:FindFirstChild("StatusUI"):Destroy()
        end
        Player.Backpack:ClearAllChildren()
        wait(5)
        Events.RestartEvent:FireClient(Player)
        Character:Destroy()
    end
end

What I suspected

When I first encountered this I believed it was due to the fact that the script didn't have enough time to get the correct string so it got the default string, but when I attempted to fix that with a wait() it had no effect. I've encountered stuff like this before and most of the time it would be due to a lack of time for the values to load properly, but that seems to not be the case.

If you would like to resolve it over discord

My Discord used is "VincenttheBaguette1#1253".

0
Have you checked if the Class.Value and Country.Value is correct? I suspect the issue is the variable or the StringValue. NotThatFamouss 605 — 2y
0
If your talking about if they meet with the requirements then yes I have and it matches correctly I probably should have stated that -_- vincentthecat1 199 — 2y
0
I'm sorry if I miss what you're saying, but is both Values conflicting? Like the Class in ReplicatedStorage is RifleMan but the class in the MainGui is Sapper? NotThatFamouss 605 — 2y
0
I was just able to fix it. I'll make an answer in a min. vincentthecat1 199 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

I found out how I could fix it and it works! The reason the class wasn't correct is still unknown to me but I did find a fix. That requires 2 scripts. The script that is shown in the question and a script that had nothing to do with the question.

Script that fixed everything

script.Parent.MouseButton1Click:Connect(function()
    local Player = game.Players.LocalPlayer
    local ValuesFolder = script.Parent.Parent.Parent.Parent.ValuesFolder
    local Country = ValuesFolder.Country
    local Class = ValuesFolder.Class 
    print(Country.Value.." "..Class.Value)
    game.ReplicatedStorage.Events.SpawnEvent:FireServer(Class.Value)
end)

Fixed question script

function onSpawnPlayer(Player)
    wait(.1)
    local MainPlayerGui = game.StarterGui["MainGui"]:Clone()
    if not Player.PlayerGui:FindFirstChild("MainGui") then
        MainPlayerGui.Parent = Player.PlayerGui
    else
        Player.PlayerGui:FindFirstChild("MainGui"):Destroy()
        MainPlayerGui.Parent = Player.PlayerGui
    end
    ReplicatedStorage.Events.SpawnEvent.OnServerEvent:Connect(function(Player, Class)
        print(Class)
        local Country = Player.PlayerGui.MainGui.ValuesFolder.Country
        MainPlayerGui:Destroy()
        Player:LoadCharacter()
        if Country.Value == "Britain" then
            if Class == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Smle-MK3"]:Clone()
                RifleManTool.Parent = Player.Backpack
            elseif Class == "Sapper" then
                local SapperTool = TeamTools.Primary["Lewis Gun"]:Clone()
                SapperTool.Parent = Player.Backpack
            end
        elseif Country.Value == "France" then
            if Class == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Berthier-07"]:Clone()
                RifleManTool.Parent = Player.Backpack
            end
        elseif Country.Value == "America" then
            if Class == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Springfield M1903"]:Clone()
                RifleManTool.Parent = Player.Backpack
            elseif Class == "Sapper" then
                local SapperTool = TeamTools.Primary["Lewis Gun"]:Clone()
                SapperTool.Parent = Player.Backpack
            end
        end
        if Country.Value == "Germany" then
            if Class == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Gewehr-98"]:Clone()
                RifleManTool.Parent = Player.Backpack
            end
        elseif Country.Value == "Austria-Hungary" then
            if Class == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Gewehr-88"]:Clone()
                RifleManTool.Parent = Player.Backpack
            end
        elseif Country.Value == "Ottoman Empire" then
            if Class == "RifleMan" then
                local RifleManTool = TeamTools.Primary["Turkish Mauser"]:Clone()
                RifleManTool.Parent = Player.Backpack
            end
        end
    end)
    function onPlayerDeath(Character)
        print("Died")
        if Player.PlayerGui:FindFirstChild("StatusUI") then
            Player.PlayerGui:FindFirstChild("StatusUI"):Destroy()
        end
        Player.Backpack:ClearAllChildren()
        wait(5)
        Events.RestartEvent:FireClient(Player)
        Character:Destroy()
    end
end

The way I fixed it was well by using the script that fired the remote event. I gave it the Class.Value and then I removed the Value in every Class.Value in the script above. I honestly should have thought of this earlier but didn't because I felt it didn't really have anything to do with the problem.

Ad

Answer this question