How do I make a script local?

  1. What do you want to achieve? Keep it simple and clear!

Making this script below local:

function onTouched(hit)
	
	wait()
	script.Parent.Color = Color3.new(1, 0.458824, 0.458824)
	script.Parent.Material = Enum.Material.Neon
	wait(0.5)
	script.Parent.Anchored = false
	
end

script.Parent.Touched:Connect(onTouched)
  1. What is the issue? Include screenshots / videos if possible!

I don’t know how to achieve this.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried adding local to the first line, thought it’d work but it didn’t.

local function onTouched(hit)
	
	wait()
	script.Parent.Color = Color3.new(1, 0.458824, 0.458824)
	script.Parent.Material = Enum.Material.Neon
	wait(0.5)
	script.Parent.Anchored = false
	
end

script.Parent.Touched:Connect(onTouched)
1 Like

Is it a local script? You can only run client code on a local script.

1 Like

Nope, it’s a server script, parented inside of the part I’m targeting.

Change it to LocalScript and it’ll work.

Alrighty, do that and put it inside the part like I did with the server script, right?

No. It’s preferred if you put local scripts in client places such as StarterGui.

1 Like

Local scripts do not run in white workspace. They only work if they are in:

Now what do you mean making it local? I don’t think I understand what you mean, you only want one player to see it? Only the player that touched the part?

Yeah, only the player that touched the part, nobody else.

To run a script locally you use a local script

This must be a Local Script in Starter Player Scripts Do this, make sure to define the path to the part on the first line:

local Part = --define path here
local player = game.Players.LocalPlayer

local function onTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == player.Name then
	wait()
	Part.Color = Color3.new(1, 0.458824, 0.458824)
	Part.Material = Enum.Material.Neon
	wait(0.5)
	Part.Anchored = false
    end
	
end

Part.Touched:Connect(onTouched)

Edit: Sorry I made a few mistakes, so recopy it now.

yeah

You have to define the path to the part. What is the part’s name, and is the part a direct child of the workspace?

Yeah, I forgot to do that. I just added game.Workspace.Red

I made a few mistakes in my previous script, this should be the updated script. Basically what I did was this. Every player has their own local script, and each of their scripts detects when the part is touched. Then the script checks if the player that touched it is the local player, if it is, then it changes the part.

local Part = game.Workspace.Red
local player = game.Players.LocalPlayer

local function onTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == player.Name then
	wait()
	Part.Color = Color3.new(1, 0.458824, 0.458824)
	Part.Material = Enum.Material.Neon
	wait(0.5)
	Part.Anchored = false
    end
	
end

Part.Touched:Connect(onTouched)
1 Like

please

Do you know the reasoning of this? It paused in mid air, the script helps though, thanks!

1 Like

Wait so it is just floating in mid air?

1 Like

Yep, it’s just standing there, menacingly.

Very strange. The only thing I can think of is that it becomes anchored, but the script wouldn’t do that. Do you have any other scripts that handle the part. Also can you test the game, step on the part, then check the properties tab while still in the game and see if the part is anchored?

Yeah I’ll check it out, hopefully I get some errors so me or you could fix it.

1 Like