What I am trying to do is set the Attribute of the Player ID inside of game. I tried workspace, then a BinaryStringValue in ReplicatedStorage, and I came to the conclusion that it is with this.
local PlayerId
game:GetService("Players").PlayerAdded:Connect(function(plr)
if PlayerId == nil then
PlayerId = plr.UserId
game.ReplicatedStorage.TownData:SetAttribute("TownOwner", plr.UserId)
print("Town owner created")
end
end)
I then tried if not PlayerId then and it still didn’t work.
If you can help, please let me know, Thanks! WE
Alright, I’m new to coding so this might not work (2nd hour seriously trying to learn how to code lol). So I see when you are stating or defining or whatever it is called the “PlayerId” variable, you leave what it is equal to blank. I read in a Roblox article that if you do this, it defaults to the boolean value of false and the variable represents a boolean. To make sure it is an integer, try changing the first line to “local PlayerId = 0.” I hope this helps.
That seems like it would work, but why? I’m new and don’t really know what nil is and I think you could explain it better than the developer hub. Use what you put as an example.
You did local PlayerId this means you was not assign any value. This would auto do nil but the problem is roblox don’t know how you will use it in your script. If you set it manually it should work. Also nil is nothing. It is like empty object
local PlayerId = nil -- by default nil
game.Players.PlayerAdded:Connect(function(plr)
local success,errormessage = pcall(function()
if PlayerId == nil then
print("Creating town owner"):Wait(2) -- incase of slow loading or any other error
if game.Players:FindFirstChild(plr.Name) and game.Players:FindFirstChild(plr.Name).UserId then
PlayerId = plr.UserId
game.ReplicatedStorage.TownData:SetAttribute("TownOwner", plr.UserId)
end)
end
if success then
print("Town owner has been created")
else
print(error.." is the error")
end
end)
end)