a bool value becomes true when S is pressed and it becomes false when S is released, and I have a projectile which I’m trying to remove whenever that value becomes true.
The problem is that the projectile is only being removed locally, so it won’t remove for the other player. https://gyazo.com/9928ac3f9df7a68bfd405ddef6d624bc --(the right side is what should be seen for both players)
I have tried removing the projectile from the serverside which I think is what I need to do but I couldn’t figure out how. Any help?
--local
local uis = game:GetService("UserInputService")
local down1 = game.Workspace.walkvals.down1
uis.InputBegan:connect(function(input)
if input.KeyCode==Enum.KeyCode.S then
down1.Value = true
end
end)
uis.InputEnded:connect(function(input)
if input.KeyCode==Enum.KeyCode.S then
down1.Value = false
end
end)
game.ReplicatedStorage.fireshot.OnClientEvent:Connect(function(newshot)
local BodyVelocity = ("BodyVelocity")
local Velocity = Instance.new(BodyVelocity, newshot)
Velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
Velocity.Velocity = Vector3.new(0,0,-60)
if down1.Value == false then
newshot:Remove()
print("down")
end
end)
-------------------------------------------------------------------
--ignore
local sm = game.Workspace.walkvals.sm
uis.InputBegan:connect(function(input)
if input.KeyCode==Enum.KeyCode.F then
sm.Value = true
end
end)
uis.InputEnded:connect(function(input)
if input.KeyCode==Enum.KeyCode.F then
sm.Value = false
end
end)
sm.Changed:Connect(function()
if sm.Value then -- true
repeat print("hi1")
game.ReplicatedStorage.fireshot:FireServer()
wait(0.4)
until not sm.Value
else
end
end)
-----------------------------------------------------------------------------
--server
game.ReplicatedStorage.fireshot.OnServerEvent:Connect(function(player)
local AlreadyTouched = false
local character = player.Character
local shothit = game.ServerStorage:WaitForChild("shothit")
local newshot = shothit:Clone()
newshot.CFrame = player.Character.Head.CFrame:ToWorldSpace(CFrame.new(0,0,-1.7))
newshot.Parent = workspace
newshot:SetNetworkOwner(player)
game.ReplicatedStorage.fireshot:FireClient(player, newshot)
end)
When a player joins, create a separate folder for them and parent it to the workspace. When firing a projectile, I suggest setting the parent of newshot to their corresponding folder in Workspace.
When you press S, fire a remoteEvent to the server
uis.InputBegan:connect(function(input)
if input.KeyCode==Enum.KeyCode.S then
down1.Value = true
game.ReplicatedStorage.RemoteEvent:FireServer()
end
end)
ServerScript which removes the parts on the server
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
local folder = game.Workspace:FindFirstChild(plr.Name.."Projectiles") -- it can be another name
if folder ~= nil then
folder:ClearAllChildren()
end
end)
This is what I have but nothing is shooting (everything should be fine on local side)
game.ReplicatedStorage.fireshot.OnServerEvent:Connect(function(player)
local AlreadyTouched = false
local character = player.Character
local shothit = game.ServerStorage:WaitForChild("shothit")
local newshot = shothit:Clone()
newshot.CFrame = player.Character.Head.CFrame:ToWorldSpace(CFrame.new(0,0,-1.7))
local folder = game.Workspace:FindFirstChild(player.Name.."shootstore")
if folder ~= nil then
newshot.Parent = folder
newshot:SetNetworkOwner(player)
game.ReplicatedStorage.fireshot:FireClient(player, newshot)
end
end)
game.ReplicatedStorage.hi12.OnServerEvent:Connect(function(plr)
local folder = game.Workspace:FindFirstChild(plr.Name.."shootstore") -- it can be another name
if folder ~= nil then
folder:ClearAllChildren()
end
end)
Also, should the folder be created in a server or local script?
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder")
folder.Parent = workspace
folder.Name = "shootstore"
game.Players.PlayerRemoving:Connect(function(plr2)
if plr == plr2 then
folder:Destroy()
end
end)
end)
It is destroying all the parts everytime you have pressed S. That is why it is not shooting anything. I thought you wanted the parts to be destroyed when the bool value turns true? You said that in the post or I read it wrong
So wait is this supposed to always delete the part no matter if the value is true or false? or only when true? because at the moment the part is always deleting
It deletes all parts you fired when you press S. Your post said to remove the parts on the server when the bool value is true and the bool value becomes true when you press S
If you’re just trying to destroy a part which has been instantiated (created) by the server when a particular key is pressed then you should make use of a RemoteEvent instance in order to faciliate communication between the server and the client which fired it.
--LOCAL
local userInput = game:GetService("UserInputService")
local replicated = game:GetService("ReplicatedStorage")
local remoteEvent = replicated:WaitForChild("RemoteEvent")
userInput.InputBegan:Connect(function(key, processed)
if processed then
return
end
if key.KeyCode == Enum.KeyCode.F then
remoteEvent:FireServer()
end
end)
--SERVER
local replicated = game:GetService("ReplicatedStorage")
local remoteEvent = replicated.RemoteEvent
remoteEvent.OnServerEvent:Connect(function(player)
local projectile = workspace:FindFirstChild("Projectile")
if projectile then
projectile:Destroy()
end
end)
If each projectile belongs to a particular player instance and only that player instance should be able to destroy/create the projectile then you would do the following.
--LOCAL
local userInput = game:GetService("UserInputService")
local replicated = game:GetService("ReplicatedStorage")
local inputStarted = replicated:WaitForChild("InputStarted")
local inputFinished = replicated:WaitForChild("InputFinished")
userInput.InputBegan:Connect(function(key, processed)
if processed then
return
end
if key.KeyCode == Enum.KeyCode.F then
inputStarted:FireServer()
end
end)
userInput.InputEnded:Connect(function(key, processed)
if processed then
return
end
if key.KeyCode == Enum.KeyCode.F then
inputFinished:FireServer()
end
end)
--SERVER
local replicated = game:GetService("ReplicatedStorage")
local inputStarted = replicated.InputStarted
local inputFinished = replicated.InputFinished
inputStarted.OnServerEvent:Connect(function(player)
local projectile = Instance.new("Part")
projectile.Anchored = true
projectile.CFrame = CFrame.new(0, 0, 0)
projectile.Name = player.Name.."'s Projectile"
projectile.Parent = workspace
end)
inputFinished.OnServerEvent:Connect(function(player)
local projectile = workspace:FindFirstChild(player.Name.."'s Projectile")
if projectile then
projectile:Destroy()
end
end)
I’ve tested both pairs of scripts and they work as intended.