Trying to make it so when on mobile when you touch the ContextActionService gui thing then your screen the fireball shoots in the direction you tapped your screen on mobile. Not sure if this works but if it doesn't can you explain to me why and how to fix it

Look at lines 67-93(local script):

local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local debounce = false
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

--Animation
local animInstance = Instance.new("Animation")
animInstance.AnimationId = "rbxassetid://04160584403"
local fireballAnim = humanoid:LoadAnimation(animInstance)


local function shootFireball(caster, mousePos)
	if caster == player then
		game.ReplicatedStorage.Events.FireBall:FireServer("Replicate",mousePos) --Show fireball for other players
	end

	local effects = game.ReplicatedStorage.FireBallEffects
	local fireBall = effects.FireBall:Clone()
	local explosion = effects.Explosion:Clone()
	local character = caster.Character

	--Fireball
	fireBall.Position = character.RightHand.Position
	fireBall.Parent = workspace
	fireBall.Sound:Play()
	local weld = Instance.new("WeldConstraint", fireBall)
	weld.Part0 = fireBall
	weld.Part1 = character.RightHand
	wait(0.45)
	weld:Destroy()
	local velocity = Instance.new("BodyVelocity", fireBall)
	velocity.Velocity = CFrame.new(fireBall.Position, mousePos).LookVector * 100

	--Explosion
	fireBall.Touched:Connect(function(hit)
		if hit:IsDescendantOf(character) or hit:FindFirstChild("ParticleEmitter") then return end
		explosion.Position = fireBall.Position
		fireBall:Destroy()
		explosion.Parent = workspace
		explosion.Sound:Play()
		if caster == player then --Make sure the damage doesnt activate when someone else casted
			game.ReplicatedStorage.Events.FireBall:FireServer(hit)
		end
		wait(1)
		explosion:Destroy()
	end)
end

--PC
UIS.InputBegan:Connect(function(key, processed)
	if processed or debounce then return end
	debounce = true

	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode.One then
			fireballAnim:Play()
			wait(0.75)
			shootFireball(player, mouse.Hit.Position)
		end
	end
	wait(1)
	debounce = false
end)

--Mobile
function onKeyPress(actionName, userInputState, inputObject, position, processed)
	if processed or debounce then return end
	debounce = true
	workspace.FireballValue.Value = workspace.FireballValue.Value + 1
	if workspace.FireballValue.Value == 1 then
	wait(0.1)
	if UIS.TouchEnabled then
	print("Activated")
	if UIS.TouchTap then
    fireballAnim:Play()
	shootFireball(player, mouse.Hit.Position)
	workspace.FireballValue.Value = workspace.FireballValue.Value - 1
	print("1 was pressed")
		wait(1)
			debounce = false
end
end
game.ContextActionService:BindAction("keyPressSpecialName", onKeyPress, true, Enum.KeyCode.One)
game.ContextActionService:SetPosition("keyPressSpecialName", UDim2.new(.5,0,-.5,0))
game.ContextActionService:SetImage("keyPressSpecialName", "rbxassetid://73737626")


game.ReplicatedStorage.Events.FireBall.OnClientEvent:Connect(function(caster, mousePos)
	if caster == player then return end --The person casting the fireball can already see it
	shootFireball(caster, mousePos) --Create the fireball
end)

1 comment

0

The ContextActionService thingy doesnt show when i use the mobile emulator on pc but maybeit shows on mobile?