so i'm making a sword;
script.Parent.Handle.Touched:Connect(function(p) if script.Parent.CanDamage.Value == true then script.Parent.CanDamage.Value = false local AttackDamage = math.random(script.Parent.MinAttackDamage.Value,script.Parent.MaxAttackDamage.Value) local Victim = p.Parent:FindFirstChild("Humanoid") if Victim == nil then return p else Victim:TakeDamage(AttackDamage) local DamageText = Instance.new("TextLabel",p.Parent.Head.DamageGui) -- Displays the damage done to the humanoid DamageText.Name = "DamageText" DamageText.Text = AttackDamage DamageText.Size = UDim2.new(1,0,1,0) DamageText.BackgroundTransparency = 1 DamageText.TextColor3 = Color3.fromRGB(255,255,255) DamageText.TextScaled = true DamageText.TextStrokeTransparency = 0.5 local DamageTextScript = game.ServerStorage.DamageTextScript:Clone() -- Will add the script for removing and deleting the copied DamageText DamageTextScript.Parent = DamageText DamageTextScript.Disabled = false wait(script.Parent.AttackDelay.Value) script.Parent.CanDamage.Value = true end end end)
i'm not sure how it can change the info stored in the function(p) part because i can't get it o attack other things if the thing it attack doesn't have a humanoid. please help me...
.Touched
events will fire whenever any block of any sort touches the part you're calling the event from. For example, if you had a .Touched event on a door, it would fire for all of the parts touching it such as the floor, door frame, player arm, leg, torso etc.
Because of this, it is important to make sure that you validate your code so that it only runs when something specific touches it. There are many ways to do this;
This will check to make sure the touch-ee has a humanoid (useful for NPCs)
part.Touched:Connect(function(part) if part.Parent:FindFirstChild'Humanoid' then --// Code here end end)
This will check to make sure the touch-ee is a player (useful if you only want players to be able to activate it)
part.Touched:Connect(function(part) local player = game:GetService'Players':GetPlayerFromCharacter(part.Parent) if player then --// Code here end end)
Hope this helped you :)