Recently I have been making a Bow model / tool for my game. Everytime I click it fires through a RemoteEvent and it continues from there. The LocalScript and ServerScript are located inside the bow and the Event is in a folder in ReplicatedStorage.
But when I shoot it fires for all players and it is really weird. I know this will do for all players. And all dummies weilding the bow as well. How would I go along to fix this?
ServScript:
--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local TweenService = game:GetService("TweenService");
local Debris = game:GetService("Debris");
local Players = game:GetService("Players")
--// Models
local Tool = script.Parent;
local Storage = Tool:FindFirstChild("Storage");
local Handle = Tool:FindFirstChild("Handle");
local Character = nil;
local Humanoid = nil
--// Values
local CanShoot = true;
--// Animations
local StillAnim = nil;
local AttackAnim = nil
--// Events
local FireEvent = ReplicatedStorage:FindFirstChild("Events"):FindFirstChild("Fire")
--[[]]----[[]]----[[]]--
--// Functions, 1
local Arg1 = function(Variable)
if Variable == "Equip" then
--[print(Variable)
Character = Tool.Parent;
Humanoid = Character:FindFirstChild("Humanoid")
StillAnim = Humanoid:LoadAnimation(Storage.IdleAnimation)
AttackAnim = Humanoid:LoadAnimation(Storage.Attack)
Humanoid.WalkSpeed = Storage.WalkSpeed.Value
if StillAnim ~= nil then
StillAnim:Play()
end
end
--\\//--
if Variable == "Dequip" then
--[print(Variable)
if Character ~= nil and StillAnim ~= nil then
Humanoid.WalkSpeed = 16
StillAnim:Stop()
end
end
end
--[[]]----[[]]----[[]]--
FireEvent.OnServerEvent:Connect(function(player, position)
if CanShoot == true then
CanShoot = false
local Arrow = Instance.new("Part");
Arrow.Parent = workspace
Arrow.Name = "Arrow"
Arrow.Size = Vector3.new(2, 2, 2)
Arrow.Anchored = true
Arrow.CanCollide = false
Arrow.BrickColor = BrickColor.new("White")
--[[local Mesh = Instance.new("SpecialMesh");
Mesh.Parent = Arrow
Mesh.MeshId = "rbxassetid://2592153631"
Mesh.Scale = Vector3.new(.0125, .0125, .0075)--]]
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = { player.Character, workspace.BossAttackParts }
local origin = Handle.Position
local direction = (position - origin).Unit * 300
--local result = workspace:Raycast(origin, direction)
local result = workspace:Raycast(origin, direction, params)
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
local bullet_clone = Arrow
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection) * CFrame.new(0, 0, -distance / 2)
bullet_clone.Parent = workspace
Handle["Bow Fire"]:Play()
AttackAnim:Play()
if result then
local part = result.Instance
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(50000)
end
end
task.wait(.1)
bullet_clone:Destroy()
task.wait(.3)
CanShoot = true
end
end)
--// Functions, 2
Tool.Equipped:Connect(function()
Arg1("Equip");
end)
Tool.Unequipped:Connect(function()
Arg1("Dequip");
end)
--[[local TweenArrow = TweenService:Create(Arrow, TweenInfo.new(.1, Enum.EasingStyle.Linear), {Position = CF.Position})
TweenArrow:Play()
Arrow.Touched:Connect(function(Hit)
local Player = Players:GetPlayerFromCharacter(Hit.Parent);
if not Player then
local NPC = Hit.Parent;
if NPC:FindFirstChild("Humanoid") and NPC.Torso.CollisionGroup ~= "Statue" then
NPC.Humanoid:TakeDamage(math.random(2100, 3000))
Debris:AddItem(Arrow, 0)
end
end
end)
Debris:AddItem(Arrow, .2)
task.wait(.4)
CanShoot = true--]]
LocScript:
--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local Players = game:GetService("Players")
--// Models
local Tool = script.Parent;
local Player = Players.LocalPlayer;
local Mouse = Player:GetMouse()
--// Values
local Time = .4;
--// Events
local FireEvent = ReplicatedStorage:FindFirstChild("Events"):FindFirstChild("Fire")
--[[]]----[[]]----[[]]--
--// Functions, 1
local Arg1 = function(Variable)
if Variable == "Click" then
local CF = Mouse.Hit.Position
FireEvent:FireServer(CF)
end
end
--// Functions, 2
Tool.Activated:Connect(function()
Arg1("Click");
end)