Fruskle
Fruskle

Reputation: 1

Part only moving to the mouse position for local user

I am developing a game in Roblox Studio and I am having a problem where when I place an item it only moves the position for the player that placed it. The player who did not place the item can only see the trap where it was in the players hand.

This is how I detect where the player wants to place the item and run the server script.

--LocalScript
script.Parent.Activated:Connect(function()
    local player = game.Players.LocalPlayer
    
    local mouse = player:GetMouse()
    local model = mouse.Target
    
    
    if mouse.Target.Name == "Grass" or mouse.Target.Name == "Bedrock" or mouse.Target.Name == "Brick" then
        game.ReplicatedStorage.TrapPlaced:FireServer(mouse.Target)
    end
end)

This is where it places the item on the ground but the position does not change for the players that do not place the item.

game.ReplicatedStorage.TrapPlaced.OnServerEvent:Connect(function(player, mouseTarget)
    local player = game.Players[player.Name]
    
    local Trap = player.Character.Trap.Trap
    
    local model = mouseTarget
    
    Trap.Position = Vector3.new(model.Position.X,model.Position.Y + 2.1,model.Position.Z)
    
    Trap.ClickDetector:Remove()
    Trap.Anchored = true
    
    Trap.Parent = workspace
    
    player.Character.Trap:Remove()
end)

Example Image

The left side is the player who placed the item and the right side is what all other players see.

Any ideas on how to make the item show in the correct position for all players?

Upvotes: 0

Views: 437

Answers (1)

Kylaaa
Kylaaa

Reputation: 7203

This is unusual, since the object was created on the server, its position should be replicating to each client properly. So something to try might be rather than passing the Mouse.Target, which is the Model that was selected by the mouse click, try passing the Mouse.Hit which is the CFrame of where the mouse click landed.

In your LocalScript :

script.Parent.Activated:Connect(function()
    local player = game.Players.LocalPlayer
    local mouse = player:GetMouse()
    
    local acceptableTargets = {
        Grass = true,
        Bedrock = true,
        Brick = true,
    }
    if acceptableTargets[mouse.Target.Name] then
        game.ReplicatedStorage.TrapPlaced:FireServer(mouse.Hit)
    end
end)

Then in your Script :

game.ReplicatedStorage.TrapPlaced.OnServerEvent:Connect(function(player, mouseCFrame)
    local Trap = player.Character.Trap.Trap
    Trap.Parent = workspace
    Trap.Position = mouseCFrame.Position + Vector3.new(0, 2.1, 0)
    Trap.ClickDetector:Remove()
    Trap.Anchored = true
    
    player.Character.Trap:Remove()
end)

Upvotes: 0

Related Questions