So I’ve made a knife for my newest game, however specifically for the normal knife I have issues getting the clients to line up. What I do is instead of making the knife function on the server I make something happen on the main players client, then to everyone else’s through a server event.
The is the clients don’t line up that well for the normal knife whereas the beam knife is perfect.
ServerEvent
Events.KnifeWeaponFunction.OnServerEvent:Connect(function(p, p1, p2, p3, p4, p5, p6, p7, p8)
local Char = p.Character
Events.KnifeWeaponFunction:FireAllClients("N/A", p2, Char, p4, p5, p6, p7, p8)
end)
ClientEvent
RemoteEvents.KnifeWeaponFunction.OnClientEvent:Connect(function(p1, p2, p3, p4, p5, p6, p7, p8)
if p3 ~= game.Players.LocalPlayer.Character then
elseif p2 == "Normal" then
local KnifeTracker = p4
local Knife = p3.Knife.Handle:Clone()
Knife.CanCollide = false
Knife.Anchored = true
Knife.CFrame = p3.Knife.Handle.CFrame
Knife.Name = "tKnife"..KnifeTracker
Knife.Parent = workspace
local ThrowLoop = Instance.new("Sound")
ThrowLoop.Name = "ThrowLoop"
ThrowLoop.Looped = true
ThrowLoop.Volume = 0.3
ThrowLoop.SoundId = "http://www.roblox.com/asset?id="..KnifeModule.Sounds.ThrowLoop[1]
ThrowLoop.Parent = Knife
ThrowLoop:Play()
local Pos, KCP, Distance = p5, p6, p7
for i = 1, Distance, 6 do
Knife.CFrame = CFrame.new(Pos, Knife.CFrame.Position) * CFrame.new(0,0,i-Distance) * CFrame.fromEulerAnglesXYZ(math.rad(i*15),math.rad(180),math.rad(360))
wait()
end
Knife.CFrame = CFrame.new(Pos, Knife.CFrame.Position) * CFrame.new(0,0,0) * CFrame.fromEulerAnglesXYZ(math.rad(90),math.rad(180),math.rad(360))
local KnifeContents = Knife:GetChildren()
for k = 1, #KnifeContents do
if KnifeContents[k]:IsA("Sound") then
KnifeContents[k]:Destroy()
end
end
end
end)
The Normal knife function, DetectHuman is a function that checks if the knife touches a player before the knife reaches the end of the ray
local Touched = false
local Knife = Tool.Handle:Clone()
Knife.CanCollide = false
Knife.Anchored = true
Knife.CFrame = Char.Knife.Handle.CFrame
Knife.Name = "tKnife"..KnifeTracker
Knife.Parent = workspace
local ThrowLoop = Instance.new("Sound")
ThrowLoop.Name = "ThrowLoop"
ThrowLoop.Looped = true
ThrowLoop.Volume = 0.3
ThrowLoop.SoundId = "http://www.roblox.com/asset?id="..Sounds.ThrowLoop[1]
ThrowLoop.Parent = Knife
ThrowLoop:Play()
local NewRay = Ray.new(Knife.CFrame.Position, (MousePos - Knife.CFrame.Position).unit * 1000)
local Hit, Pos = game.Workspace:FindPartOnRayWithIgnoreList(NewRay, GenerateIgnoreList(Knife))
local Distance = (Pos - Knife.CFrame.Position).magnitude
spawn(function()
local P, K, D = Pos, Knife.CFrame.Position, Distance
Events.KnifeWeaponFunction:FireServer("N/A", "Normal", "N/A", KnifeTracker, P, K, D)
end)
for i = 1, Distance, 6 do
DetectHuman(Knife, Char)
Knife.CFrame = CFrame.new(Pos, Knife.CFrame.Position) * CFrame.new(0,0,i-Distance) * CFrame.fromEulerAnglesXYZ(math.rad(i*15),math.rad(180),math.rad(360))
wait()
end
Knife.CFrame = CFrame.new(Pos, Knife.CFrame.Position) * CFrame.new(0,0,0) * CFrame.fromEulerAnglesXYZ(math.rad(90),math.rad(180),math.rad(360))
local KnifeContents = Knife:GetChildren()
for k = 1, #KnifeContents do
if KnifeContents[k]:IsA("Sound") then
KnifeContents[k]:Destroy()
end
end
if Hit then
if game.Players:GetPlayerFromCharacter(Hit.Parent) == nil and game.Players:GetPlayerFromCharacter(Hit.Parent.Parent) == nil then
Knife.Anchored = true
Events.KnifeWeaponFunction:FireServer("N/A", "WallHit", "N/A", KnifeTracker)
else
local char1, char2 = game.Players:GetPlayerFromCharacter(Hit.Parent), game.Players:GetPlayerFromCharacter(Hit.Parent.Parent)
if char1 ~= nil or char2 ~= nil and char1 ~= Char and char2 ~= Char then
local GetChar = nil
if Hit.Parent:FindFirstChild("Humanoid") and not Hit.Parent.Parent:FindFirstChild("Humanoid") then
GetChar = Hit.Parent
elseif Hit.Parent.Parent:FindFirstChild("Humanoid") and Hit.Parent:IsA("Accessory") and not Hit.Parent:FindFirstChild("Humanoid") then
GetChar = Hit.Parent.Parent
end
if GetChar ~= nil then
local GetTorso = GetChar:FindFirstChild("Torso")
local GetHuman = GetChar:FindFirstChild("Humanoid")
if GetTorso and GetHuman then
if Char then
spawn(function()
Events.KnifeWeaponFunction:FireServer("N/A", "ThrowHitSound", "N/A", GetTorso)
Module.PlaySound(Sounds.ThrowHit[1], GetTorso, 0.5)
end)
if game.ReplicatedStorage.Content.GameInProgress.Value == true then
if game.ReplicatedStorage.Content.GameMode.Value == "Wicked Murderer" then
Events.KnifeDamage:FireServer(GetHuman, 100)
elseif game.ReplicatedStorage.Content.GameMode.Value == "Freeze Tag" then
if GetChar:FindFirstChild("Role") and GetChar.Role.Value == "Bystander" then
Events.FreezePlayerKnife:FireServer(GetChar)
end
else
-- any other gamemode not specified
Events.KnifeDamage:FireServer(GetHuman, 100)
end
elseif game.ReplicatedStorage.Content.GameInProgress.Value == false then
Events.KnifeDamage:FireServer(GetHuman, 100)
-- when a game is not occuring
end
end
Events.KnifeWeaponFunction:FireServer("N/A", "DestroyKnife", "N/A", KnifeTracker)
end
end
end
end
end