Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Touched event works in localscript but not server-side?

I've made a script to shoot a projectile out of the top of my tool. I want to damage players when they come into contact with it.

The problem is my positioning doesn't work right server-side due to latency issues. So I have to move the projectile into the workspace and position it using a localscript. Now if I run this block of code in a localscript it works perfectly fine. It prints test when anything is touched by the projectile.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local shootEvent = ReplicatedStorage:WaitForChild("projectileFire")
local projectiles = game.Players.LocalPlayer.Backpack.Projectiles:GetChildren()
local blowTip = script.Parent.Body.Tube.TubeTop.CFrame
local projectile = projectiles[1]
projectile.Parent = game.Workspace
projectile.CFrame = blowTip * CFrame.new(2, 0, 0)

local thrust = Instance.new("BodyVelocity")
local direction = blowTip.LookVector
thrust.Parent = projectile
thrust.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
thrust.Velocity = CFrame.new(player2.Character.HumanoidRootPart.CFrame.p, mouse.Hit.p).lookVector * 200

projectile.Touched:Connect(
    function(hit)
        print("Test")
    end
)

game:GetService("Debris"):AddItem(thrust, 0.1)
game:GetService("Debris"):AddItem(projectile, 3)

Now I have a remote set up to send the required data for propelling my projectile. Here is the code for both the local and server scripts currently.

-- LOCALSCRIPT CODE
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local shootEvent = ReplicatedStorage:WaitForChild("projectileFire")
local projectiles = game.Players.LocalPlayer.Backpack.Projectiles:GetChildren()
local blowTip = script.Parent.Body.Tube.TubeTop.CFrame
local projectile = projectiles[1]
projectile.Parent = game.Workspace
projectile.CFrame = blowTip * CFrame.new(2, 0, 0)

shootEvent:FireServer(mouse.Hit.p, projectile)

-- SERVER SCRIPT CODE
local function shot(player, mouse, projectile)
    local blowTip = script.Parent.Body.Tube.TubeTop.CFrame
    local thrust = Instance.new("BodyVelocity")
    local direction = blowTip.LookVector
    thrust.Parent = projectile
    thrust.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
    thrust.Velocity = CFrame.new(player.Character.HumanoidRootPart.CFrame.p, mouse).lookVector * 200

    projectile.Touched:Connect(
        function(hit)
            print("Test")
        end
    )

    game:GetService("Debris"):AddItem(thrust, 0.1)
    game:GetService("Debris"):AddItem(projectile, 3)
end

game.ReplicatedStorage.projectileFire.OnServerEvent:Connect(shot)

It's firing and propelling the projectiles as expected, but not printing my string. That leads me to believe that there's something going on with the touched event. I can't for the life of me figure out what that is. Any help is appreciated!

2 answers

projectile.Touched:Connect(function(hit)
print("Test")
end)

Your touched script was placed improperly and was causing errors. This is why it didn't run when you touched.

1 comment

1

Actually, there's nothing wrong with his Touched event. It works the exact same as the code you provided.

I found the solution if anyone else is having this problem. I moved everything over to my server-side script so it handles the movement from the player's backpack into the workspace and propelling it.

Now this was not working because the server-script did not get the proper CFrame of the tip of my tool. So I passed that through the remote along with the mouse position. It works just as intended now.

Here's the code.

-- CLIENT SIDE
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local shootEvent = ReplicatedStorage:WaitForChild("projectileFire")
local blowTip = script.Parent.Body.Tube.TubeTop.CFrame
shootEvent:FireServer(mouse.Hit.p, blowTip)

-- SERVER SIDE 
local function shot(player, mouse, blowTip)
    local projectiles = player.Backpack.Projectiles:GetChildren()
    local projectile = projectiles[1]
    projectile.Parent = game.Workspace
    projectile.CFrame = blowTip * CFrame.new(2, 0, 0)

    local thrust = Instance.new("BodyVelocity")
    local direction = blowTip.LookVector
    thrust.Parent = projectile
    thrust.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
    thrust.Velocity = CFrame.new(player.Character.HumanoidRootPart.CFrame.p, mouse).lookVector * 200

    projectile.Touched:Connect(
        function(hit)
            print("Test")
        end
    )

    game:GetService("Debris"):AddItem(thrust, 0.1)
    game:GetService("Debris"):AddItem(projectile, 3)
end

game.ReplicatedStorage.projectileFire.OnServerEvent:Connect(shot)

This discussion is preserved as it was. New answers, comments, and votes are closed.