I’m trying to make a FNaF-esque game, and I’m using a configuration to hold the AI numbers. The problem is that for some reason, an attribute just deletes when I fire an event that starts the game. The console gives off nothing regarding this behaviour.
I’m sure there’s a simple workaround for this, I only want to know what causes the attribute to get deleted.
Startup script:
script.Parent.MouseButton1Click:Connect(function()
local yellowai = ailist.Yellow.AIText.Text
local darkai = ailist.Dark.AIText.Text
local redai = ailist.Red.AIText.Text
local roseai = ailist.Rose.AIText.Text
game.Lighting.Blur.Enabled = false
game.ReplicatedStorage.Events.Begin:FireServer(game.Players.LocalPlayer,yellowai,darkai,redai,roseai)
script.Parent.Parent.Parent.Enabled = false
end)
Reciever script
local ailist = game.Workspace.Mascots.AIList
game.ReplicatedStorage.Events.Begin.OnServerEvent:Connect(function(plr,yellowai,darkai,redai,roseai)
ailist:SetAttribute("yellow",tonumber(yellowai))
ailist:SetAttribute("dark",tonumber(darkai))
ailist:SetAttribute("red",tonumber(redai))
ailist:SetAttribute("rose",tonumber(roseai))
end)
Your RemoteEvent.OnServerEvent callback automatically receives the Player instance of the client who called RemoteEvent:FireServer. By sending that argument again, you cause yellowai to assume the value of that same Player instance, of which cannot be converted to a number via tonumber. tonumber will return nil, by which Instance:SetAttribute interprets as an instruction to delete the attribute:
To avoid any further headaches with invalid tonumber arguments, I recommend removing the ability for users to input numbers on their own, and instead use an increment or decrement button. You can also use the string pattern%D+ with string.gsub on TextBox:GetPropertyChangedSignal(“Text”) to force your text box to become numerical input only. Ultimately, add or 0 to the new attribute value expression to ensure exploiters or further oversights don’t cause AI values to disappear