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

"if" statement, does not return output. how would i change the script so it returns the output?

this code is supposed to give the "iceFruit" to the player IF they have the value "1". however, the code doesn't advance past where it outputs "hey". is it a problem with the second if statement? and how would i fix it?

UPDATE: SOLUTION HAS BEEN FOUND, THANKS FOR EVERYONE'S EFFORTS.

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local function onPlayerAdded(player)
	local iceFruit = RS.DFs:FindFirstChild("Ice_Moves"):Clone()
	local Fruit = player:WaitForChild("OtherValues"):FindFirstChild("Fruit")
	
	local function onCharacterAdded(character)
	if player.OtherValues.HasFruit == false then
		print("No fruit given")
	else
		print("Has fruit")
		print(Fruit.Value)
		if Fruit.Value == 1 then
			print("ice given")
			iceFruit.Parent = player.Backpack
		end
	end
end
		
	if player.Character then
		onCharacterAdded(player.Character)
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end

for i, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end

Players.PlayerAdded:Connect(onPlayerAdded)

this is the code where the value is given:

-- Data Service

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local GameValues = Instance.new("Folder")
	GameValues.Name = "GameValues"
	GameValues.Parent = player
	
	local OtherValues = Instance.new("Folder")
	OtherValues.Name = "OtherValues"
	OtherValues.Parent = player
	
	local HasFruit = Instance.new("BoolValue")
	HasFruit.Name = "HasFruit"
	HasFruit.Parent = OtherValues
	
	local fruit = Instance.new("NumberValue")
	fruit.Name = "Fruit"
	fruit.Parent = OtherValues
	

	--data save
	
	local data, data2

	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-HasFruit",player.OtherValues.HasFruit.Value)
		data2 = myDataStore:GetAsync(player.UserId.."-fruit",player.OtherValues.Fruit.Value)	
	end)

	if success then
		HasFruit.Value = data
		fruit.Value = data2
		
		print("Data loaded!")
	else
		print("There was an error loading data.")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()	
		myDataStore:SetAsync(player.UserId.."-HasFruit",player.OtherValues.HasFruit.Value)
		myDataStore:SetAsync(player.UserId.."-fruit",player.OtherValues.Fruit.Value)
	end)
	
	if success then
		print ("Save success!")
	else
		print ("There was an error saving.")
		warn(errormessage)
	end
	
end)

10 comments

0

If Fruit is a NumberValue, you'll want to change line 11 to be 'if Fruit.Value == 1 then', with no parentheses around the 1

0

Otherwise if it's a StringValue, then instead of printing "hey" on line 10, print Fruit.Value to see what value it actually is

0

i tried that and it doesnt work

0

Did you try what I wrote in my second comment? If Fruit.Value isn't "1", what is it?

0

it equals 0

0

Shouldn’t it be HasFruit.Value == false?

0

nope, its supposed to be HasFruit.Value == true but that doesnt work.

0

You wrote “not” which flips HasFruit.Value, remove the operator to ensure the logic stays direct.

0

okay, so now i did if HasFruit.Value == false then print("no fruit") else print("Has Fruit") if Fruit.Value == 1 then print("Give Ice") and it still doesnt work

0

when i tell it to print the Fruit's value, it always outputs "0". Although, my player's fruit value is 1.

1 answer

-- Data Service

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)

    local GameValues = Instance.new("Folder")
    GameValues.Name = "GameValues"
    GameValues.Parent = player

    local OtherValues = Instance.new("Folder")
    OtherValues.Name = "OtherValues"
    OtherValues.Parent = player

    local HasFruit = Instance.new("BoolValue")
    HasFruit.Name = "HasFruit"
    HasFruit.Parent = OtherValues

    local fruit = Instance.new("NumberValue")
    fruit.Name = "Fruit"
    fruit.Parent = OtherValues


    --data save

    local data, data2

    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId.."-HasFruit")--u just copy and pasted the setasync but changed setasync to getasync
        data2 = myDataStore:GetAsync(player.UserId.."-fruit")   --your only supposed to give the string
    end)

    if success then
        HasFruit.Value = data
        fruit.Value = data2

        print("Data loaded!")
    else
        print("There was an error loading data.")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()  
        myDataStore:SetAsync(player.UserId.."-HasFruit",player.OtherValues.HasFruit.Value)
        myDataStore:SetAsync(player.UserId.."-fruit",player.OtherValues.Fruit.Value)
    end)

    if success then
        print ("Save success!")
    else
        print ("There was an error saving.")
        warn(errormessage)
    end

end)

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