Help with a script

ok so im fairly new to scripting, and im working on a tds type game to help me with my skills. But i ran into this bug, i cant place my tower or rotate it on mobile

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local towers = ReplicatedStorage:WaitForChild("Towers")
local events = ReplicatedStorage:WaitForChild("Events")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local Camera = workspace.CurrentCamera
local gui = script.Parent
local towerToSpawn = nil
local canPlace = false
local rotation = 0

local function MouseRaycast(exclude) 
	local MousePosition = UserInputService:GetMouseLocation()
	local MouseRay =Camera:ViewportPointToRay(MousePosition.X, MousePosition.Y)
	local raycastParams = RaycastParams.new()
	
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = exclude
	
	local raycastResult = workspace:Raycast(MouseRay.Origin, MouseRay.Direction * 1000, raycastParams)
	

	return raycastResult
end
local function RemovePlaceholderTower()
	if towerToSpawn then
		towerToSpawn:Destroy()
		towerToSpawn = nil
		rotation = 0
	end
end
local function AddPlaceholderTower(name)
	local towerExsits = towers:FindFirstChild(name)
	if towerExsits then
		RemovePlaceholderTower()
		towerToSpawn = towerExsits:Clone()
		towerToSpawn.Parent = workspace.Towers
		
		for i, object in ipairs(towerToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Towers"
				object.Material = Enum.Material.ForceField
	end
end
		end
end

local function ColorPlaceholderTower(color)
	for i, object in ipairs(towerToSpawn:GetDescendants()) do
		if object:IsA("BasePart") then
			object.Color = color

		end
	end
end
	

gui.Spawn.Activated:Connect(function()
	AddPlaceholderTower("Slinger")
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
if towerToSpawn then
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if canPlace then
				spawnTowerEvent:FireServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
				RemovePlaceholderTower()
		end
	elseif input.KeyCode == Enum.KeyCode.R then
		rotation += 90
	
end

	end
end)

RunService.RenderStepped:Connect(function()
	if towerToSpawn  then
		local result = MouseRaycast({towerToSpawn})
		if result and result.Instance then
			if result.Instance.Parent.Name == "TowerArea" then
				canPlace = true
				ColorPlaceholderTower(Color3.new(0,1,0))
			else
				canPlace = false
				ColorPlaceholderTower(Color3.new(1,0,0))
			end
			local x = result.Position.X
			local y = result.Position.Y + towerToSpawn.Humanoid.HipHeight + (towerToSpawn.PrimaryPart.Size.Y / 2)
			local z = result.Position.Z
			local cframe = CFrame.new(x,y,z) * CFrame.Angles(0, math.rad(rotation), 0)
			towerToSpawn:SetPrimaryPartCFrame(cframe)
		end
	end
end)

i dont know how to allow mobile to place down tower and rotate it.

Looks like you are attempting to get inputs using desktop functions.

For example:

elseif input.KeyCode == Enum.KeyCode.R then
		rotation += 90
end

This will not work because the UserInputService is trying to find the keyboard Keycode R. Mobile does not use keycodes. This goes with Mousebutton events as well.

use ContextActionService instead of UIS or GetMouse, it is the only way currently to detect mobile inputs :slight_smile:

1 Like