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

[FIXED] String value returns nil?

I'm making a spin system for my magic game. And I need to let a server side script know what magic the player got. So I made a string value called "ChosenMagic", and the script that gives the player a magic will change it's value to the name of the magic. Then a folder that includes all the magics will look at the value and check to see if any of the magics matches that value, if it does then it'll clone it into the players backpack, if not then it'll give off an error. And it gives me this error:

is not a valid member of Folder ReplicatedStorage.Magics Even though the string value's value isn't a space, the script does change it to whatever magic the player got. I don't know why this is happening. Anyways,

EDIT: I changed the local script. And now it gives me this error:

Players.Desmondo1.PlayerGui.Menu.Spinning.Spin.Spinning:83: invalid argument #3 (string expected, got nil) - Client -

Edit again: I fixed that error, but it still gives me the same error as I started off with. (I've updated the code to what it currently is)

Edit once again: The problem was that I was making the value change on the clients side. Hence, only the client can see it. So the server still saw the value as nil. Apparently, you can pass parameters through remote events so by just passing the ChosenMagic parameter through to the server script. I was able to make it work.


Local script


local AlreadyClicked = false
local Amount = 0.003
local LoopTimes = script.Parent.LoopTimes


local YourMagic = script.Parent.Parent.YourMagic

local RS = game:GetService("ReplicatedStorage")
local StringValues = RS.StringValues


script.Parent.MouseButton1Down:Connect(function()
	if game.Players.LocalPlayer.Backpack.Spins.Value == 0 then
		local NES = script.Parent.Parent.NotEnoughSpins
		NES.Visible = true
		
		wait(2)
		for i=1, 20 do
			
			NES.BackgroundTransparency = NES.BackgroundTransparency + 0.05
			NES.TextTransparency = NES.TextTransparency + 0.05
			wait(0.1)
		end
		
		
		NES.Visible = false
		NES.BackgroundTransparency = 0
		NES.TextTransparency = 0
		return
	end
	
	if AlreadyClicked == true then
		return
        --print("AlreadyClicked")
	end	
	
	AlreadyClicked = true
	RS.RemoteEvents.TakeAwaySpins:FireServer()
	
	local Magics = {"Fire","Water","Nature","Void","Wind","Lightning","Curse","Demon","Fire","Water","Nature","Fire","Fire","Water","Nature","Wind","Wind","Wind","Lightning","Fire","Fire","Water","Water","Fire"}
	
	for i=1, 15 do
		
		if LoopTimes.Value == 8 then
			Amount = 0.1
		end	
		
		if LoopTimes.Value == 12 then
			Amount = 0.15
		end
		
		if LoopTimes.Value == 13 then
			Amount = 0.5
		end
		
		if LoopTimes.Value >= 14 then
			LoopTimes.Value = 0
			Amount = 0.003
			AlreadyClicked = false
		end
		
	--local RandomNumber = math.random(1, #Magics)
	ChosenMagic = Magics[math.random(1, #Magics)]
	
		YourMagic.Text = ChosenMagic
		
		local Number = math.random(1,2)
		if Number == 2 then
			local Sound = Instance.new("Sound")
			Sound.SoundId = "rbxassetid://6475635118"
			Sound.Parent = workspace
			Sound.PlaybackSpeed = 0.8
			Sound.Volume = 0.5
			Sound.MaxDistance = 10
			Sound:Play()
			
		else if Number == 1 then
				
				local Sound = Instance.new("Sound")
				Sound.SoundId = "rbxassetid://6475619109"
				Sound.Parent = workspace
				Sound.PlaybackSpeed = 0.8
				Sound.Volume = 0.5
				Sound.MaxDistance = 10
				Sound:Play()
			end
		end
		
		
		
		
		wait(Amount)
		LoopTimes.Value = LoopTimes.Value + 1
	end
	
	
	game.ReplicatedStorage.StringValues.ChosenMagic.Value = ChosenMagic
	
	RS.RemoteEvents.GiveMagic:FireServer()
end)

Server script


game.ReplicatedStorage.RemoteEvents.TakeAwaySpins.OnServerEvent:Connect(function(player)
	
	player.Backpack.Spins.Value = player.Backpack.Spins.Value - 1
end)

game.ReplicatedStorage.RemoteEvents.GiveMagic.OnServerEvent:Connect(function(Player)
	local Backpack = Player.Backpack
	local ChosenMagic = game.ReplicatedStorage.StringValues.ChosenMagic
	
	
	local Magics = game.ReplicatedStorage.Magics
	
	local GivenMagic = Magics[ChosenMagic.Value]:Clone()
	GivenMagic.Parent = Backpack
end)

1 comment

0

I SOLVED IT!

2 answers

local Magics = StringValues:GetChildren()

local ChosenMagic = Magics[math.random(1, #Magics)]


print(ChosenMagic.Value)

3 comments

0

pretty sure you can't use :getChildren() on a table

0

Did you actually read his code?

0

oh. thought it was a table, sorry ;D

change this to:

Magics = {Fire,Water,Nature,Void,Wind,Lightning,Curse,Demon,Fire,Water,Nature,Fire,Fire,Water}

this:

Magics = {"Fire","Water","Nature","Void","Wind","Lightning","Curse","Demon","Fire","Water","Nature","Fire","Fire","Water"}

5 comments

0

Thanks so much, I'm such an idiot lol.

0

Wait, now I'm getting another error: Players.Desmondo1.PlayerGui.Menu.Spinning.Spin.Spinning:83: invalid argument #3 (string expected, got nil)

0

try tostring(thing you want)

0

Line 83 was giving me the error because I forgot to remove the .Name on it. Now it gives me the same error that I started out with.

0

By the way, the error is on the server script.

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