I need help doing a punch mechanic

Im quite new to roblox coding, and so i tried making a punch mechanic for my game and everything went well but i cant seem to figure out why i cant see the spheres, i get no errors, can someone help me? Heres my entire code, hopefully it isnt bad and someone can understand it. (its a local script in StarterCharacterScripts)

local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded
local Humanoid = character:FindFirstChild("Humanoid")
local inputService = game:GetService("UserInputService")
local trash = game:GetService("Debris")
local debounce = false
local punched = {}
local tweenService = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
local sphere = game.ReplicatedStorage:WaitForChild("Sphere1"):Clone()

local function SpawnPart()
	local part = Instance.new("Part")
	part.Parent = workspace.Parts
	part.BrickColor = BrickColor.new("Really red")
	part.Size = Vector3.new(4,5,4)
	part.Anchored = true
	part.CanCollide = false
	part.Transparency = .8
	part.CFrame = character:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0,0,-2)
	part.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid and humanoid ~= Humanoid and not punched[hit.Parent] then
			humanoid:TakeDamage(10)
			script.hit:Play()
			sphere.Parent = workspace.Parts
			sphere.Transparency = 0
			sphere.CFrame = hit.CFrame
			tweenService:Create(sphere, tweeninfo, {Size = Vector3.new(4,4,4), Transparency = 1})
			sphere.Parent = trash
			punched[hit.Parent] = true
		end
	end)
	wait(.1)
	part.Parent = trash
	punched = {}
end

inputService.InputBegan:Connect(function(input, _gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and debounce == false then
		debounce = true
		SpawnPart()
		wait(.3)
		debounce = false
	end
end)

Because the sphere is immediately parented to trash.

Also, damaging players should be handled by the server.

1 Like

Ohhh, i thought the tween service went first and when it completed the code below runs, also as i mentioned im quite new to this so idk alot of stuff. Thanks for the help!

If you want to wait for the tween to complete, you would have to use :Completed event on the tween.

And I just noticed, you’re just creating the tween and not playing it, you would have to assign it to a variable, then play it.

local SphereTween = tweenService:Create(sphere, tweeninfo, {Size = Vector3.new(4,4,4), Transparency = 1})
SphereTween:Play()
-- With this you can wait for it to complete
SphereTween.Completed:Wait()

And since everything is structured in one code block like this, waiting for the tween to complete will cause the rest of the code to be delayed, so you could put everything in the .Touched function in a different thread (with Coroutine or Task).

Yeah i realized short after that the tween wasnt playing cause i didnt play it, thanks again. But im a bit unsure how to do damage server sided and why i would need to do that. Im guessing but does it have to do with exploiters?

You can do so with e.g Remote Events. And it has to do more with Server-sided logic and client replication, I’m going to explain it as simply as I can:
If you do anything inside a local script (e.g, damage a player), only you’ll be able to see it. - (Note: Operations on your own humanoid automatically replicate to the server).
Remove events allow for a client-server communication, and if the server executes an operation (damages player), then other people are going to see it.

For more information, I’ve found this topic which explains it in more detail.

Thank you, i hope you have a nice day!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.