My weapon only works on NPC's and not players - lua

So basically I made a weapon it flings people after you hit them but it only works on NPC's and not players. Could someone explain to me why this is?
local power = 5
local function fling(character)
local humanoid = character:FindFirstChildOfClass("Humanoid")
local rootpart
if humanoid then
humanoid:ChangeState(11)
rootpart = humanoid.RootPart
end
if not rootpart then
rootpart = character.AssemblyRootPart
end
if rootpart then
rootpart.Velocity = Vector3.new(math.random(-100, 100), math.random(50, 100), math.random(-100, 100)) * 10
end
end
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local slash = script:WaitForChild("Slash")
local debounce = false
local idle = false
local PlayersHit = {}
local playerService = game:GetService("Players")
local plr = playerService:GetPlayerFromCharacter(tool.Parent)
local Datastore2 = require(game.ServerScriptService:WaitForChild("Modules").Datastore2)
local defaulthits = 0
local hitds = Datastore2("hits",plr)
tool.Activated:Connect(function()
if idle == false and debounce == false then
debounce = true
local humanoid = tool.Parent:WaitForChild("Humanoid")
local animtrack = humanoid:LoadAnimation(slash)
animtrack:Play()
wait(1)
debounce = false
idle = false
end
end)
handle.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= tool.Parent then
if debounce == true and PlayersHit[hit.Parent] == nil then
fling(hit.Parent)
hitds:Increment(1, defaulthits)
debounce = false
script.Parent.Handle.hit:Play()
end
end
end)
This is my current code. I do not know what to do I'm pretty sure its something to do with the fling script.

I found out how instead of humanoid:changestate(11) i used humanoid.PlatformStand = true

Related

Part doesn't gives damage with Touched Event

I made a code for my game, it's a type of punch, with a hitbox made with a common Part. I proggrammed to the part give some damage according with the player strenght. It didn't work, no errors in output. Code here:
local rep = game:GetService("ReplicatedStorage")
local debounceDMG = true
rep.Combate.Soco.OnServerEvent:Connect(function(plr)
local math = math.random(1,2)
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char.Humanoid
local lanim = char.Humanoid:LoadAnimation(script.Left)
local lanim1 = char.Humanoid:LoadAnimation(script.Animation)
local hitbox = Instance.new("Part")
hitbox.Parent = workspace
hitbox.Transparency = 1
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Size = Vector3.new(2.5,2.5,2.5)
game:GetService("RunService").Heartbeat:Connect(function()
if math == 2 then
hitbox.Position = char.LeftHand.Position
elseif math == 1 then
hitbox.Position = char.RightHand.Position
end
end)
if math == 2 then
lanim:Play()
elseif math == 1 then
lanim1:Play()
end
hitbox.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h then
if h ~= Humanoid and debounceDMG then
debounceDMG = false
h.Health -= plr.Data.Forca.Value + 5
task.wait(.5)
debounceDMG = true
end
end
end)
task.wait(.3)
hitbox:Destroy()
end)
You use "if h~=Humanoid" will always look for Non Humanoid, your code will never reach the Health deduction. Try to code below if working
hitbox.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil and debounceDMG then
debounceDMG = false
h.Health -= plr.Data.Forca.Value + 5
task.wait(.5)
debounceDMG = true
end
end)

Two Codes Confliting each other, how to fix it?

I made 2 codes of special abilitys and if i activate the SuperFireSphere code, then the Combat code damage stop working, the hitbox don't spawn in workspace and i don't know how to fix it.
SuperFireSphere Code:
local ts = game:GetService("TweenService")
local replicated = game.ReplicatedStorage
local onepiecepowers = replicated.OnePiecePowers
local Info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local ss = game:GetService("ServerStorage")
local FireWind = ss.FireWind:Clone()
local db = true
onepiecepowers.AceGiantFire.OnServerEvent:Connect(function(Player, Hit)
local char = Player.Character or Player.CharacterAdded:Wait()
local SuperFireSphere = Instance.new("Part")
local hrpp = char.PrimaryPart
local currentcam = workspace.Camera
currentcam.CameraSubject = char.Humanoid
currentcam.CameraType = Enum.CameraType.Scriptable
SuperFireSphere.Shape = Enum.PartType.Ball
SuperFireSphere.Material = Enum.Material.Neon
SuperFireSphere.BrickColor = BrickColor.new("New Yeller")
SuperFireSphere.Anchored = true
SuperFireSphere.CanCollide = false
task.wait(.2)
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Velocity = CFrame.lookAt(hrpp.CFrame.Position, Hit.Position).LookVector * 150
hrpp.Position += Vector3.new(0, 50 , 0)
hrpp.Anchored = true
SuperFireSphere.Parent = workspace
FireWind:SetPrimaryPartCFrame(hrpp.CFrame)
FireWind.Parent = workspace
SuperFireSphere.Position = char.PrimaryPart.Position + Vector3.new(0,15,0)
for i = 1, 15 do
task.wait(.1)
SuperFireSphere.Size += Vector3.new(2,2,2)
end
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
local tween = ts:Create(parts, Info, {Transparency = 1})
tween:Play()
end
end
local connect = SuperFireSphere.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= char.Humanoid and db == true then
db = false
hit.Parent.Humanoid.Health -= char.DevilFruitPoints.Value
task.wait(2)
db = true
end
end)
local FireParticle = ss.Fire:Clone()
FireParticle.Parent = SuperFireSphere
SuperFireSphere.Anchored = false
bv.Parent = SuperFireSphere
task.wait(2)
hrpp.Anchored = false
FireWind.Parent = ss
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
parts.Transparency = 0
end
end
task.wait(5)
SuperFireSphere:Destroy()
connect:Disconnect()
end)
Combat:
local replicated = game:GetService("ReplicatedStorage")
local combatevent = replicated.Combat
local servstore = game:GetService("ServerStorage")
local runservice = game:GetService("RunService")
local db = true
combatevent.OnServerEvent:Connect(function(Player)
local char = Player.Character or Player.CharacterAdded:Wait()
local hbox = Instance.new("Part")
hbox.Size = Vector3.new(1.5,1.5,1.5)
hbox.Anchored = true
hbox.CanCollide = false
local connection = runservice.Heartbeat:Connect(function(dt)
hbox.Position = char.RightHand.Position
end)
local tconnection = hbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= char.Humanoid and db then
db = false
hit.Parent.Humanoid.Health -= char.Strenght.Value
task.wait(.1)
db = true
end
end)
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://10497535355"
animation.Parent = char.Humanoid
local loadanim = char.Humanoid:LoadAnimation(animation)
loadanim:Play()
hbox.BrickColor = BrickColor.new("Really red")
hbox.Transparency = 0.5
hbox.Name = Player.Name
print(hbox.Name)
task.wait(.2)
hbox:Destroy()
tconnection:Disconnect()
loadanim:Stop()
connection:Disconnect()
end)
Does someone have any suggestion to help me to fix that??
.........................................................

How to score when killing an NPC

When I kill an NPC, my score should increase, but it does not.
Here's my code. It is located under my NPC model.
local Humanoid = script.Parent.Humanoid
function PwntX_X()
local tag = Humanoid:findFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local Leaderstats = tag.Value:findFirstChild("leaderstats")
if Leaderstats ~= nil then
Leaderstats.Score.Value = Leaderstats.Score.Value + 250 --Change Money to the stat that is increased.
wait(0.1)
script:remove()
end
end
end
end
Humanoid.Died:connect(PwntX_X)
And here's my code for the leaderboard
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("BoolValue",plr)
stats.Name = "leaderstats"
local score = Instance.new("IntValue", stats)
score.Name = "Score"
score.Value = 0
end)
Not sure, but looks like
score.Value = Leaderstats.Score.Value

Roblox - attempt to index nil with 'leaderstats'

Can someone tell me how can I fix this error that shows up when I run my script? Thanks
line 4: Workspace.Slide1.PointsPart.Script:4: attempt to index nil with 'leaderstats'
script.Parent.Touched:Connect(function(hit)
local player = hit.Parent:FindFirstChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr.leaderstats.Points.Value >= 0 then
wait()
script.Disabled = true
script.Parent.Transparency = 1
script.Parent.CanCollide = false
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value +5
wait(0.5)
script.Parent.Transparency = 1
script.Parent.CanCollide = false
script.Disabled = false
end
end)
The Touched event fires for anything that touches the part. You are not handling the case that a part isn't a child of a Player's Character.
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if not plr then
return
end
if plr.leaderstats.Points.Value >= 0 then
You could change it to this
script.Parent.Touched:Connect(function(hit)
local player = hit.Parent:FindFirstChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and plr.leaderstats.Points.Value >= 0 then
wait()
script.Disabled = true
script.Parent.Transparency = 1
script.Parent.CanCollide = false
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value +5
wait(0.5)
script.Parent.Transparency = 1
script.Parent.CanCollide = false
script.Disabled = false
end
end)
the compiler will calculate the instance "plr" first,
if it isn't nil, then the compiler will calculate "plr.leaderstats.Points.Value >= 0"
and this process called "Short-Circuit Evaluation"

How to spawn a part at the current mouse position in Roblox Lua?

I've been working on a script for Roblox. Here's the code:
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Activation =
Instance.new("Sound",game.Players.LocalPlayer.Character.Head)
local char = Player.Character
local hum = char.Humanoid
local root = char.HumanoidRootPart
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
Activation.SoundId = "rbxassetid://1581091676" --Plays Mangekyou Sharingan Activation Sound.
Activation:Play()
wait(0.3)
game.Players.LocalPlayer.Character.Head.face.Texture = "rbxassetid://76285632" --When F is pressed, face texture changes to sharingan decal.
game:GetService("Chat"):Chat(Player.Character.Head, "Mangekyou Sharingan!")
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
Activation.SoundId = "rbxassetid://1580990602" --Plays Amaterasu Activation Sound.
Activation:Play()
game:GetService("Chat"):Chat(Player.Character.Head, "Amaterasu!")
local Target = Instance.new("Part") --makes a part
Target.Parent = game.Workspace
Target.Position = Vector3.new(Mouse.target.Position) --makes the part spawn where the mouse is
Target.Transparency = 1
Target.Anchored = true
Target.CanCollide = false
local Amaterasu = Instance.new("Fire")
Amaterasu.Parent = game.Workspace.Part
Amaterasu.Color = Color3.new(0,0,0)
Amaterasu.SecondaryColor = Color3.new(0,0,0) --amaterasu properties
Amaterasu.Size = 25
local R = Instance.new("RocketPropulsion") --rocket propulsion, parents amaterasu
R.Parent = Amaterasu
R.MaxThrust = 300
R.ThrustP = 30
R:Fire()
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.G then
game.Players.LocalPlayer.Character.Head.face.Texture = "rbxassetid://22557247" --When G is pressed, face texture changes back to normal.(leaves face blank isnt working :/)
end
end)
-----------------------------
What I need help with is spawning a the part that parents the amaterasu fire at the current position of my mouse in game. I have researched and tried both Target.Position = Vector3.new(Mouse.target.Position), and Target.Position = Vector3.new(Mouse.Hit).
These both don't work and the end result is the part spawning in the middle of the baseplate no matter where my mouse's position is in game.
What we need is a CFrame. So we'll do:
Target.CFrame = CFrame.new(Mouse.target.Position)
Here is the ticket
local Part = Instance.new("Part",game.Workspace);
Part.CFrame = Mouse.Hit;
Target.CFrame = CFrame.new(Mouse.target.Position)
That Should Work

Resources