Attempt to call a nil value & passed value is not a function?

My brother and I are bored, so both of us decided to create a TDS type of game. We’re creating a GUI, but our current script doesn’t work.
Code:

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("PlaceTower")
local runService = game:GetService("RunService")
runService.RenderStepped:Connect(function() --test purposes
	print("Hello")
end)
local plrService = game:GetService("Players")
local plr = plrService.LocalPlayer
local place = function(tower, pos) --The place function
	tower.Position = pos + Vector3.new(0, tower.Position.Y/2, 0)
end
script.Parent.MouseButton1Up:Connect(function()
	local mouse = plr:GetMouse()
	local turret = script.Parent.Parent:WaitForChild("ViewportFrame"):WaitForChild("Viewport")
	local toPlace = turret:Clone()
	toPlace.Parent = game.Workspace
	toPlace.Name = "Turret"
	local connection = runService.RenderStepped:Connect(place(toPlace, mouse.Hit.p)) --Errors here, place is a function and what's nil?
end)

What’s wrong here?

EDIT: Showed line with error to find the problem easily

Nil is if something doesnt exists or isnt declared. Please show us the place where you declared the variable (Place) EDIT: SORRY FOUND IT

I was just commenting on the line to help you find it, well at least you found it

Can i ask you what the full error is?

Those are the only 2 errors.

  • Attempt to connect failed: Passed value is not a function: errors once, when I connect the event

  • Attempt to call a nil value: Errors multiple times

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("PlaceTower")
local runService = game:GetService("RunService")
local plrService = game:GetService("Players")
local plr = plrService.LocalPlayer

script.Parent.MouseButton1Up:Connect(function()
	local mouse = plr:GetMouse()
	local pos = mouse.Hit.p
	local turret = script.Parent.Parent:WaitForChild("ViewportFrame"):WaitForChild("Viewport")
	local toPlace = turret:Clone()
	toPlace.Parent = game.Workspace
	toPlace.Name = "Turret"
	local connection = runService.RenderStepped:Connect(function(toPlace, pos)
		toPlace.Position = pos + Vector3.new(0, toPlace.Position.Y/2, 0)
	end)
end)

Alright, I’ll modify the code a bit when I get the chance since it won’t give me the desired results.

you have the function as a variable, just use anormal function instead

Tried that earlier as well, didn’t work

-- Inside MouseButton1Up
-- before connection
local function Place()
 toPlace.Position = pos + Vector3.new(0, toPlace.Position.Y/2, 0)
end
RenderStepped:Connect(Place)

I already told you, that didn’t work.

works for me in studio, your pro blem probably has to do with the turret itself instead of anything else

The turret is just a union with no scripts, I made it myself

you’re cloning something called “Viewport” in “ViewportFrame”, is that the union?

1 Like

So uh it kind of works except it says toPlace is a number, why does this happen?
Code:

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("PlaceTower")
local runService = game:GetService("RunService")
runService.RenderStepped:Connect(function()
   print("Hello")
end)
local plrService = game:GetService("Players")
local plr = plrService.LocalPlayer
local place = function(tower, pos)
tower.Position = pos + Vector3.new(0, tower.Position.Y/2, 0)
end
script.Parent.MouseButton1Up:Connect(function()
   local mouse = plr:GetMouse()
   local pos = mouse.Hit.p
   local turret = script.Parent.Parent:WaitForChild("ViewportFrame"):WaitForChild("Viewport")
   local toPlace = turret:Clone()
   toPlace.Parent = game.Workspace
   toPlace.Name = "Turret"
   local conection = runService.RenderStepped:Connect(function(toPlace)
      local pos = mouse.Hit.p
      toPlace.Position = pos + Vector3.new(0, toPlace.Position.Y/2, 0)
   end)
end)

renderstepped’s only function return is step which is the time since the last renderstepped fire actually just use the thing I posted it will work fine

2 Likes
local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("PlaceTower")
local runService = game:GetService("RunService")
local plrService = game:GetService("Players")
local plr = plrService.LocalPlayer
function place(tower, pos) --The place function
	tower.Position = pos + Vector3.new(0, tower.Position.Y/2, 0)
end

script.Parent.MouseButton1Up:Connect(function()
	local mouse = plr:GetMouse()
	local mousePos = mouse.Hit.p
	local turret = script.Parent.Parent:WaitForChild("ViewportFrame"):WaitForChild("Viewport")
	local connection = runService.RenderStepped:Connect(function(step)
		local toPlace = turret:Clone()
		toPlace.Parent = game.Workspace
		toPlace.Name = "Turret"
		toPlace.Position = mousePos + Vector3.new(0, toPlace.Position.Y/2, 0)
	end)
end)

Alright, I’ll try it out later. Can’t do it right now, I’m on my study laptop

It works! Thanks! Now I have to figure out how to keep it down on the ground…

this is what I use: ```
-- CLIENT
uis.InputBegan:Connect(function(k,gpe)
	if not gpe and k.UserInputType == Enum.UserInputType.MouseButton1 then
		local ml = mouse.UnitRay
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Whitelist
		params.FilterDescendantsInstances = {
			workspace.Towers
		}
		local ray = workspace:Raycast(ml.Origin,ml.Direction*1000,params)
		print("raycasting")
		if ray and ray.Instance then
			print("xd")
			--local otherThing = script.Frame:Clone()
			--otherThing.Parent = script.Parent.Parent
			--local nVector = vector/2
			--otherThing.Position = UDim2.new(scale.X*0.5,0,scale.Y,0)
			local root = ray.Instance.Parent:FindFirstChild("HumanoidRootPart") or ray.Instance.Parent.Parent:FindFirstChild("HumanoidRootPart") or ray.Instance.Parent.Parent.Parent:FindFirstChild("HumanoidRootPart") 
			local gui = script.Parent.Parent.Parent.Parent.BillboardGui
			gui.Adornee = root
			gui.Frame.Visible = not gui.Frame.Visible
		end
		if selected ~= "" then
			TowerPlaceRemote:FireServer(selected,ml)
			selected = ""
		end
	end
end)
-- SERVER
TowerPlaceRemote.OnServerEvent:Connect(function(player,tower,pos)
	local TowerModel = Towers:FindFirstChild(tower)
	local TowerConfigurationObject = GEC:FindFirstChild(tower)
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = getPlayerCharacters()
	local ray = workspace:Raycast(pos.Origin,pos.Direction*1000,params)
	print(ray.Position,ray.Instance)
	local Placement = require(TowerConfigurationObject.Placement)
	local Config = TowerConfigurationObject.Config
	if ray.Position and ray.Instance then
		if ray.Instance.Name ~= Placement["Required_Placement"] then return end
		if TowerModel then
			print("Tower found.")
			local c = TowerModel:Clone()
			local Script
			local Animations = {}
			if TowerConfigurationObject.UseDefaultTowerScript.Value == true then
				Script = TowerConfigurationObject.Parent.DefaultTowerScript:Clone()
				print(Script:GetFullName())
				for i,v in pairs(TowerConfigurationObject.Animations:GetChildren()) do
					v:Clone().Parent = Script
				end
			else
				Script = TowerConfigurationObject.UseDefaultTowerScript:FindFirstChildOfClass("Script"):Clone()
				print(Script:GetFullName())
				for i,v in pairs(TowerConfigurationObject.Animations:GetChildren()) do
					v:Clone().Parent = Script
				end
			end
			Config:Clone().Parent = Script
			Script.Parent = c
			Script.Disabled = false
			local num = https:GenerateGUID(false)
			local tag = Instance.new("StringValue")
			tag.Parent = c
			tag.Name = "Identifier"
			tag.Value = c.Name .. tostring(num)
			local tag = Instance.new("ObjectValue")
			tag.Parent = c
			tag.Name = "Owner"
			tag.Value = player
			DataService.AddToPlayerData(player,"Placed_Towers",c.Name .. tostring(num),c)
			if Placement then
				c.HumanoidRootPart.Position = Vector3.new(ray.Position.X,Positions[Placement["Required_Placement"]]-(Placement.Custom_Y or 0),ray.Position.Z)
			else
				c.HumanoidRootPart.Position = Vector3.new(ray.Position.X,Positions["Required_Placement"],ray.Position.Z)
			end
			c.HumanoidRootPart.Anchored = true
			c.Parent = workspace.Towers
		end
	end
end)