Creating an Item System Similar to Doors or Pressure

I’m trying to create a system of items with holding animations and functions for each item. This system would hopefully work similar to Doors’ or Pressure’s.

My current system uses a tool welded to the player’s hand using a Motor6D

The tool is unable to stick to the player’s hand however, and just stays on the ground in its original position.

Here is a video of the problem:

Here is the script inside StarterPlayerScripts that recognizes when a player has a tool added to them and welds it to the player(this is likely the script that is malfunctioning because the object is never welded to the player’s hand.):

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local M6D = Instance.new("Motor6D")
		M6D.Parent = char.RightHand
		M6D.Part0 = char.RightHand
		M6D.Name = "ToolGrip"
		
		char.ChildAdded:Connect(function(child)
			if child:IsA("Tool") and child:FindFirstChild("BodyAttach") then
				M6D.Part1 = child.BodyAttach -- "BodyAttach is the name of the tool's main part which attaches to the player
			end
		end)
	end)
end)

Here is the script inside the tool that plays the hold animation in the player:

local anim = Instance.new("Animation")
anim.Parent = script.Parent
anim.AnimationId = "rbxassetid://126578299845572"

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local load = char:WaitForChild("Humanoid"):LoadAnimation(anim)
script.Parent.Equipped:Connect(function()
	load:Play()
end)

script.Parent.Unequipped:Connect(function()
	load:Stop()
end)

I’ve had issue with this before, try unanchoring the flashlight model/tool

Hi! I tested your script and it worked, tool is welded to hand.

Try check tool’s directory. Maybe something is in wrong place?

It should look like this:
image

Edit:
I think i found out why this is happens

Event CharacterAdded fires when character’s model added in workspace
Script is child of player, and it loading in game after model
That is while script loading CharacterAdded already fired.

To solve this you can just put Script in ServerScriptService

its already a local script
putting it in starter character would make more sense since its would make getting parts of the character easier + the script would load after the character loads so using script.Parent is easier instead of .CharacterAdded:Wait()

Hi! I have not so good English skills so i don’t undarstand what exactly you said, but anyway.

I don’t think that’s LocalScript because it have:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)

These events can’t be detected from LocalScript

I don’t see difference between CharacterAdded:Wait() and script.Parent. Both is easy
But Using CharacterAdded:Wait() can free up space in RAM, becouse it will be on server, not client

“These events can’t be detected from LocalScript”

This is false. Both of those events can be perfectly detected within a LocalScript. It’s only that the local player won’t be detected by Players.PlayerAdded due to them already being in the server. LocalScripts are downloaded from a Roblox game server, which requires the client to have established a connection with said game server. Only then will the LocalScript begin executing, which is why they cannot detect the local player’s entry into the game. All other future players can be detected, however.

" I don’t see difference between CharacterAdded:Wait() and script.Parent. Both is easy
But Using CharacterAdded:Wait() can free up space in RAM, becouse it will be on server, not client"

This is entirely negligible and not reliant on what end of the network the script is running on.

1 Like

If your code is indeed within a Script instance, know that these instances cannot run Luau code under StarterPlayerScripts, only LocalScripts can. As @oke60000 stated, and after my elaboration, it is not viable for you to use the same code within a LocalScript. The next best option that continues to uphold your original design is to simply move the script into ServerScriptService (do not change to a LocalScript). There is a known edge-case with connecting to locally-hosted servers before the server is able to begin executing your scripts, so we can adjust the code to accommodate that:

local Players = game:GetService("Players")


local function onCharacterAdded(character: Model)
    local motor6D  = Instance.new("Motor6D")
    motor6D.Name   = "ToolGrip"
    motor6D.Part0  = character.RightHand
    motor6D.Parent = motor6D.Part0

    character.ChildAdded:Connect(function(child: Instance)
        if not child:IsA("Tool") then
            return
        end

        local bodyAttach = child:FindFirstChild("BodyAttach")
        if not bodyAttach then
            return
        end

        motor6D.Part1 = bodyAttach
    end)
end

local function onPlayerAdded(player: Player)
    local character = player.Character

    if character then
        onCharacterAdded(character)
    end

    player.CharacterAdded:Connect(onCharacterAdded)
end


for _, player in Players:GetPlayers() do
    task.spawn(onPlayerAdded, player)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Hmm I didn’t notice that. Well, thanks for information!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.