Functions "activating" without anything calling it

The problem is there is a function connected to a remote function but the script that invoke the remote function only happens when the conditions are met with one of them being the player left click on a specific model. However, the function runs when the player joins the game effectively causing the game to be unplayable. Here are the code of the function.

function Tower.Spawn(plr, name, cframe, previous)
	local AllowedToSpawn = Tower.CheckSpawn(plr, name)
	if AllowedToSpawn then

		local newTower
		if previous then
			previous:Destroy()
			newTower = RepStorage.Towers.Upgrades[name]:Clone()
		else
			newTower = RepStorage.Towers[name]:Clone()
		end
		newTower:SetPrimaryPartCFrame(cframe)
		newTower.Parent = workspace.Towers
		newTower.HumanoidRootPart:SetNetworkOwner(nil)

		local bodyGyro = Instance.new("BodyGyro")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		bodyGyro.D = 0
		bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
		bodyGyro.Parent = newTower.HumanoidRootPart

		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Tower"
			end
		end
		plr.Money.Value -= newTower.Status.Cost.Value
		plr.TowerPlaced.Value += 1
		coroutine.wrap(Tower.Attack)(newTower,plr)

		return newTower
	else
		warn("Requested tower does not exist", name)
		return false
	end
end

And here is the function that this function is calling.

function Tower.CheckSpawn(player, name)
	local towerExist = RepStorage.Towers:FindFirstChild(name, true)
	if towerExist then
		if towerExist.Status.Cost.Value <= player.Money.Value then
			if player.TowerPlaced.Value < maxTower then
				return true
			else
				warn("Player has reached the max limit")
			end
		else
			warn("Player cannot afford")
		end
	else
		warn("That tower does not exist")
	end
	return false
end

I have tried using an if function before the function call to check if the parameters exist but got ignored and I have tested the remote function to see if they are invoked but it never got invoked.

1 Like

Hi. Are you still having this issue or have you resolved it yet? If not, we probably need to see more code. You say the function fires when the player loads, so you probably have a :PlayerAdded function I would imagine. What does that look like? Also, you say this is only supposed to happen when a player left clicks a model. Can we see that code as well?