Why does this script not change an int value?

  1. What do you want to achieve? Keep it simple and clear!

I want to change a int value inside of a tool to be 1 less that it was originally from the client to the server using a remote function.

  1. What is the issue? Include screenshots / videos if possible!

For some reason, my script doesn’t want to change an int value.

I have a tool inside lighting which is cloned into backpack and when it is equipped it does an error in the output which I will show below in a sec.

Inside of the tool is the int value I want to change the value of and a local script with the following code (bits in comment is stuff that is less important):

-- Config

local blocks = {game.Lighting.Dirt_Block}

---------------------

-- Variables
local equipped = false
--local toolsModule = require(game.Lighting.Tools.ToolsModule)
local tool = script.Parent

--script.Parent.Equipped:Connect(function()
--	equipped = true
--end)
--script.Parent.Unequipped:Connect(function()
--	equipped = false
--end)

-- Place block on mouse button 2 click
game:GetService("UserInputService").InputBegan:Connect(function(input)
	
	if equipped == true then
	
		--local mouse = game.Players.LocalPlayer:GetMouse()
	
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			
			--local block = blocks[math.random(1, #blocks)]
			--local blockPosition = Vector3.new(math.floor(mouse.Hit.X / 3 + 0.5) * 3, math.floor(mouse.Hit.Position.Y / 3 + 0.5) * 3, 0)
			
			--local blockTaken = false
			--for i, v in pairs(workspace:GetChildren()) do
			--	if v:IsA("Model") then
			--		if v.PrimaryPart.Position == blockPosition then
			--			if string.find(v.Name, "Block") ~= nil then
			--				blockTaken = true 
			--			end
			--		end
			--	end
			--end
			
			--if blockTaken == false then
				--game.ReplicatedStorage.PlaceBlock:InvokeServer(block, blockPosition)
				if tool:FindFirstChild("Amount") then
					game.ReplicatedStorage.ChangeValue:InvokeServer(tool, tool.Amount.Value - 1)
				end
			--end
		
		end
		
	end

end)

I have a remote function inside replicated storage named “ChangeValue” and a script inside server script service with this code inside it:

game.ReplicatedStorage.ChangeValue.OnServerInvoke = function(plr, tool, value)
	
	tool.Amount.Value = value
	
end

Here is the output:

[[
15:16:46.536 - ServerScriptService.ChangeToolAmount:3: attempt to index nil with 'Amount'
15:16:46.543 - Stack Begin
15:16:46.543 - Script 'ServerScriptService.ChangeToolAmount', Line 3
15:16:46.544 - Stack End

  15:16:46.576 - ServerScriptService.ChangeToolAmount:3: attempt to index nil with 'Amount'
15:16:46.577 - Stack Begin
15:16:46.578 - Script 'Players.Warrenandravan.Backpack.Grass_Block.LocalScript', Line 45-- Grass_Block is the tool name
15:16:46.579 - Stack End
]]
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve looked for solutions on the dev hub but nothing is what I’m looking for.
This has happened occasionally before btw.

Thanks for reading my first post! Help would be appreciated lots.

Well, the error says that you are attempting to index nil with 'FinfdFirstChild' on line 3 in the script that is in serverstorage.

I don’t see FindFirstChild in the server code you’ve posted, so I assume there is a line with FindFirstChild somewhere in the script, but you haven’t posted it. Because that line errors, the function you posted won’t work either, as the script stops running. To get help with your problem, you’ll need to post more of that script.

Sorry, I accidentally put the old output in. I’ve edited the post. Thanks for telling me. :slight_smile:

Is the tool cloned to the player’s backpack in a server script? If the clone is created on the client, it doesn’t exist on the server.

1 Like

Yes. Thanks for telling me! 30charlimit @RoBoPoJu