Script not working as a localscript

This code works perfectly when I use it in a normal script but ceases to function as a localscript without giving me any sort of errors or clues. Any help would be greatly appreciated!

local gravel = script.Parent
local texture = script.Parent.Texture
local isTouched = false

local function PlayerTouched()
	if isTouched == false then
		gravel.Transparency = 0.1
		texture.Transparency = 0.1
		isTouched = true
		wait(0.2)
		gravel.Transparency = 0.2
		texture.Transparency = 0.2
		wait(0.2)
		gravel.Transparency = 0.3
		texture.Transparency = 0.3
		wait(0.2)
		gravel.Transparency = 0.4
		texture.Transparency = 0.4
		wait(0.2)
		gravel.Transparency = 0.5
		texture.Transparency = 0.5
		wait(0.2)
		gravel.Transparency = 0.6
		texture.Transparency = 0.6
		wait(0.2)
		gravel.Transparency = 0.7
		texture.Transparency = 0.7
		wait(0.2)
		gravel.Transparency = 0.8
		texture.Transparency = 0.8
		wait(0.2)
		gravel.Transparency = 0.9
		texture.Transparency = 0.9
		wait(0.2)
		gravel.Transparency = 1
		texture.Transparency = 1
		gravel.CanCollide = false
		wait(5)
		gravel.Transparency = 0
		texture.Transparency = 0
		gravel.CanCollide = true
		isTouched = false
	end
end

gravel.Touched:connect(PlayerTouched)

Scripts and LocalScripts have been made to act differently. Changing properties from a LocalScript won’t replicate the change to the server and so other players won’t see it if that’s your problem

Local scripts do not work when placed as a descendant of workspace.

By the way heres a smaller version of that script:

local gravel = script.Parent
local texture = script.Parent.Texture
local isTouched = false

local function PlayerTouched()
if isTouched == false then
isTouched = true
repeat
task.wait(0.2)
gravel.Transparency += 0.1
texture.Transparency += 0.1
until
gravel.Transparency == 1
texture.Transparency == 1

gravel.CanCollide = false
task.wait(5)
gravel.Transparency = 0
texture.Transparency = 0
gravel.CanCollide = true
         end
    end
end
gravel.Touched:Connect(PlayerTouched)
1 Like

I know what the difference is between the two, it’s just that when I do a localscript it does not function whatsoever

What do you mean by “it doesn’t function”?

1 Like

I’ll make a video to show you, one sec

(Normal script): Screen Recording 2022-01-03 at 6.45.38 PM
(Localscript): Screen Recording 2022-01-03 at 6.46.57 PM

I see now. Where is the LocalScript located? If it’s in the workspace it won’t work unless it’s in a player’s character

Make a RemoteEvent in ReplicatedStorage, then fire the remote event when the gravel is touched.

Make a Local script somewhere (not the workspace or a part inside of the workspace)
and put this in the local script:

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(texture, gravel, isTouched)
if isTouched == false then
isTouched = true
repeat
task.wait(0.2)
gravel.Transparency += 0.1
texture.Transparency += 0.1
until
gravel.Transparency == 1
texture.Transparency == 1

gravel.CanCollide = false
task.wait(5)
gravel.Transparency = 0
texture.Transparency = 0
gravel.CanCollide = true
         end
    end
end
end)

In the script inside of the gravel put this:

local gravel = script.Parent
local texture = script.parent.Texture
local isTouched = false
gravel.Touched:Connect(function()
game.ReplicatedStorage.RemoteEvent:FireAllClients(gravel, texture, isTouched)
end)
1 Like

It was inside the part, is there a way to modify it to be in Workspace?

No LocalScripts in the workspace just don’t run so you need to adjust it to work being located in a player related “place” like StarterPlayerScripts

Where do I put the localscript?

You can put it in StarterGui, StarterPlayerScripts, or StarterCharacterScripts.

Got the error “Players.Turned_Away.PlayerScripts.LocalScript:10: Incomplete statement: expected assignment or a function call”

Replace the line and the line above with: gravel.Transparency == 1 and texture.Transparency == 1

LocalScripts wont run inside certain locations in which Scripts do(for example workspace) although there’re a few exclusions like the player character. LocalScripts run for a single client therefore they wont replicated to the server(unless remotes are used for server communication). Due to the gravel variable at the top, which refers to script.Parent I assume the LocalScript is inside a part in workspace which is probably causing the issue. Instead the code above should be placed inside StarterCharacterScripts which will ensure it runs(I also applied some modifications and cleaned the code, although the behavior should remain the same):

--LocalScript inside StarterCharacterScripts
local Character = script.Parent 
local Humanoid = Character:WaitForChild("Humanoid")

--debounces
local touched = {} 

Humanoid.Touched:Connect(function(hit)
	--name the gravel parts "gravel"
	if hit.Name == "gravel" and not table.find(touched, hit) then 
		local texture = hit.Texture
		table.insert(touched, hit)
		for i = 0.1, 1, 0.1 do 
			hit.Transparency = i 
			texture.Transparency = i 
			task.wait(.2)
		end
		hit.CanCollide = false
		task.wait(5)
		hit.Transparency = 0
		texture.Transparency = 0
		hit.CanCollide = true
		local index = table.find(touched, hit)
		if index then 
			table.remove(touched, index)
		end
	end
end)
1 Like

you probably put script in wrong place. Put script in character,starterplayerscripts etc. and set:
gravel = workspace...

Wdym
ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

Remove line 9 and line 10(the one that had the error on it) and replace them with that line.

Btw in the local script you should also put this line after the remote event line:

gravel.Touched:Connect(function()
-- And at the end of the script add another end)