You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? I want to make a tool that throws a shuriken when activated. If the shuriken hits an NPC or a player, a text label appears, showing how much damage that NPC or player had taken.
-
What is the issue? I used a remote function to return the damage and the target position. The remote function only returns when the shuriken has been destroyed (by debris or by hitting a NPC/player), which is detected by a repeat-until loop. The problem is when multiple shurikens are thrown and some hit an NPC/player, the remote function doesn’t return anything until the last shuriken is destroyed.
-
What solutions have you tried so far? I have tried replacing the repeat-until loop with ChildRemoved event but the remote function returns before the shuriken is destroyed so it becomes nil.
Serverscript.Script:
local ServerStorage = game:GetService("ServerStorage")
local Debris = game:GetService("Debris")
game.ReplicatedStorage.ThrowShuriken = function(plr, target, startPos, mousePos)
local shuriken = ServerStorage.Shuriken:Clone()
shuriken.Name = plr.Name.."Shuriken"
shuriken.Position = startPos
shuriken.Parent = workspace
local bodyVelocity = Instance.new("BodyVelocity", shuriken)
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Velocity = (mousePos - startPos).unit*100
local bodyAVelocity = Instance.new("BodyAngularVelocity", shuriken)
bodyAVelocity.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
bodyAVelocity.AngularVelocity = shuriken.CFrame:ToWorldSpace(CFrame.new(0, 5, 0)).Position - shuriken.Position
shuriken.Touched:Connect(function(a)
if a:FindFirstAncestorOfClass("Model") and a:FindFirstAncestorOfClass("Model") ~= plr.Character and a:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid") then
shuriken.CanTouch = false
a:FindFirstAncestorOfClass("Model").Humanoid:TakeDamage(math.random(24, 26))
shuriken:Destroy()
end
end)
Debris:AddItem(shuriken)
repeat wait() until not shuriken:IsA("BasePart") -- The line here only checks if the last shuriken thrown was destroyed or not although the "shuriken" variable is created every time this remote function fires
if shuriken.CanTouch then
return damage, target.Position
else
return nil, nil
end
end
StarterPack.Shuriken.LocalScript:
local canThrow = true
local mouse = game.Players.LocalPlayer:GetMouse()
script.Parent.Activated:Connect(function()
if canThrow then
spawn(function()
canThrow = false
wait(0.5)
canThrow = true
end)
local damage, targetPos = game.ReplicatedStorage.ThrowShuriken:InvokeServer(mouse.Target, script.Parent.Handle.Position, mouse.Hit.p)
if damage and targetPos then
local position, OnScreen = game.Workspace.CurrentCamera:WorldToScreenPoint(targetPos)
if OnScreen then
game.Players.LocalPlayer.PlayerGui.ScreenGui.DamageLabel.Position = Udim2.new(0, position.X, 0, position.Y)
game.Players.LocalPlayer.PlayerGui.ScreenGui.DamageLabel.Visible = true
wait(0.5)
game.Players.LocalPlayer.PlayerGui.ScreenGui.DamageLabel.Visible = false
end
end
end
end)
This post might not be clear, since this is my first post. I won’t mind if you gave feedback on it so I can improve