How to improve my Interaction Code

Make sure to use

local players = game:GetService("Players")
local Player = players.LocalPlayer or players.PlayerAdded:wait()

When requesting the local player object on the client.

And the use this to get the player’s character

local Character = player.Character or player.CharacterAdded:wait()

Rather than using wait(), as shown here in your code

local player = game.Players.LocalPlayer
local character = player.Character
wait()
local head = character:WaitForChild("Head")
local rootpart = character:WaitForChild("HumanoidRootPart")

Wait should be avoided. It’s good practice. You can read more here:


Why to avoid using wait()