Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Variable Concatenation is nil?

While trying to concatenate PlayerReputation like so: Player.Name .. "'s Reputation is: " .. PlayerReputation. PlayerReputation has already been set, and I even have a script that sets it as 0 if it is nil. Here is the code:

local DataStoreService = game:GetService("DataStoreService")
local GameData = DataStoreService:GetDataStore("Reputation")

game.Players.PlayerAdded:connect(function(Player)
	print(Player.Name .. " has joined the game")
	print("Geting Reputation for " .. Player.Name)
	local PlayerReputation = GameData:GetAsync(Player.Name .. "Reputation")
	if PlayerReputation then
		print(Player.Name .. " has no Reputation... it was set as 0")
		GameData:SetAsync(Player.Name .. "Reputaion", 0)
		local PlayerReputation = 0
	end
	print(Player.Name .. "'s Reputation is: " .. PlayerReputation)
	local Reputation = Instance.new("IntValue")
	Reputation.Name = Player.Name
	Reputation.Value = PlayerReputation
	Reputation.Parent = game.ServerStorage.PlayerRep
	print("Reputation has been loaded for " .. Player.Name)
end)

game.Players.PlayerRemoving:connect(function(Player)
	local SavedReputation = GameData:GetAsync(Player.Name .. "Reputation")
	local Reputation = game.ServerStorage.PlayerRep[Player.Name].Value
	if SavedReputation ==  not Reputation then
		GameData:SetAsync(Player.Name .. "Reputaion", Reputation)
	end
end)

Thanks!

1 answer

✓ Accepted answer

In your code, you used "if PlayerReputation then". This makes it so if it ISN'T nil it will set to 0. So the value is never set to 0, hence it always being nil. A few more mistakes I also fixed. (Thanks @Spongocardo for catching those).

local DataStoreService = game:GetService("DataStoreService")
local GameData = DataStoreService:GetDataStore("Reputation")

game.Players.PlayerAdded:connect(function(Player)
    print(Player.Name .. " has joined the game")
    print("Geting Reputation for " .. Player.Name)
    local PlayerReputation = GameData:GetAsync(Player.Name .. "Reputation")
    if not PlayerReputation then -- Added not.
        print(Player.Name .. " has no Reputation... it was set as 0")
        GameData:SetAsync(Player.Name .. "Reputation", 0) -- Fixed spelling mistake
        PlayerReputation = 0 -- Removed local
    end
    print(Player.Name .. "'s Reputation is: " .. PlayerReputation)
    local Reputation = Instance.new("IntValue")
    Reputation.Name = Player.Name
    Reputation.Value = PlayerReputation
    Reputation.Parent = game.ServerStorage.PlayerRep
    print("Reputation has been loaded for " .. Player.Name)
end)

game.Players.PlayerRemoving:connect(function(Player)
    local SavedReputation = GameData:GetAsync(Player.Name .. "Reputation")
    local Reputation = game.ServerStorage.PlayerRep[Player.Name].Value
    if SavedReputation ==  not Reputation then
        GameData:SetAsync(Player.Name .. "Reputation", Reputation) -- Fixed spelling mistake
    end
end)

2 comments

0

There seems to be a spelling mistake in the word "Reputation" on lines 10 and 25. Also, on line 11, you don't want to make the PlayerReputation variable local as it has already been defined earlier on line 7.

0

Ok I fixed them as well

This discussion is preserved as it was. New answers, comments, and votes are closed.