CFraming a Part on local client

Hi there. I’m wanting to build a game. A part of the game is that you have to swipe a keycard in a part to open the vault. but what I am having trouble with is that when i swipe the card. a door underneath you goes down then back up, I want the door to be only on the client who swiped the card. But instead its server side.

local player = game.Players.LocalPlayer
script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == script.Parent.KeycardValue.Value then
			wait(1)
			local door = script.Parent.Parent:FindFirstChild("door")
			for i=1, 200 do 
				door.CFrame = door.CFrame * CFrame.new(0, -0.2, 0)
				wait(0)
			end
			wait(5)
			for i=1, 200 do 
				door.CFrame = door.CFrame * CFrame.new(0, 0.2, 0)
				wait(0)
			end
		end
	end)

Im very sorry if I did not explain enough. Ide be happy to ask any questions. Thank You

Well first off is this on a local script?

no it is not on a local script. It is in a script

So your main problem is that you’re doing your changes on a script which does things server side, a local script does things client side. also a normal script is not able to know game.Players.LocalPlayer because the script is server side and not client side.

But, I’m pretty sure you need to keep the function on server side and move the CFrame stuff on a local script.

how would i do that? Event? (30char)

Yep, so you want to catch the keycard being swiped on the server and use a remote event to do the changes on the local script.

got it. so where do i put the scripts?

I’m pretty sure you have your script in the part that’s catching the card so keep that there. Put your local script in StarterPlayerScripts which is in StarterPlayer

Where should i put the remote event?

This is what i have. It does not work and im not sure why

script


script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == script.Parent.KeycardValue.Value then
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		local Players = game:GetService("Players")

		local remoteEvent = ReplicatedStorage:WaitForChild("Vault")

		local function onPlayerAdded(player)
			-- Fire the remote event
			remoteEvent:FireClient(player)
		end
		Players.PlayerAdded:Connect(onPlayerAdded)
	end
	end)

localscript

game.ReplicatedStorage.Vault.OnClientEvent:connect(function()
	wait(1)
local door = script.Parent.Parent:FindFirstChild("door")
for i=1, 200 do 
	door.CFrame = door.CFrame * CFrame.new(0, -0.2, 0)
	wait(0)
end
wait(5)
for i=1, 200 do 
	door.CFrame = door.CFrame * CFrame.new(0, 0.2, 0)
	wait(0)
end
end)

Ok so this going to be a lot of words.

  1. In the script you’re using PlayerAdded. PlayerAdded detects when a player joins the game which is not what you need.

  2. You need to find the player who has the Keycard. Now I’m guessing your keycard is a tool, when your tool is equipped its stored inside the character.

  3. Roblox has a thing called GetPlayerFromCharacter which basically will help you find the player by knowing their character.

So instead of:

do

local keycard = hit.Parent
local character = keycard.Parent
local player = game.Players:GetPlayerFromCharacter(character)
remoteEvent:FireClient(player)

unfortunately there’s another issue with this:

	if hit.Parent.Name == script.Parent.KeycardValue.Value then

so when your saying if hit.Parent.Name == your basically asking if the object that touched the part is named keycard for example so change: script.Parent.Keycard.Value thing to “YOUR KEYCARD NAME HERE” (make sure to keep ") now lastly if you want to keep your keycard value thing, under the last thing I talked about do
if hit.Parent.KeycardValue.Value == something then
^ Make sure keycard value is inside the keycard tool.
sorry if that was confusing

No it was not confusing at all. Thank you very much

I just realized this but I think your local script, like the cframing might have an issue or might not. I would print something in the local script just to make sure its getting called by the remote event once your done fixing your stuff, if its getting called and still doesn’t work I would just pull a tutorial on cframing. sorry for not saying this earlier.

It Works!:+1: Thanks a lot. I will tag your message