Why cant i access a script inside of serverscriptservice?

I have a script inside a part, that is supposed to disable a startercharacterscript. ive tried multiple things, like accessing SSS itself and using playerscripts. I need to know how.

1 Like

You can only access ServerScriptService with ServerSided Scripts (Regular Scripts) if you’re trying to access ServerScriptService using local scripts, well, you can’t.

well thats the thing, im using a normal script

Oh, well now I’m a bit confused :d

so the context is there’s a script in a proximity prompt that when activated it should be disabling a script in SSS

After testing, I was able to disable a script in SSS, so, the problem seems to probably be somewhere within your code, mind showing your code?

Are there any errors in the output? It’d be helpful if you showed us the script as well.

the exact code is

local part = script.Parent.Parent
local prompt = script.Parent
local anim = script.Carry
local anim2 = script.PickUp 

local distanceZ = -2.7
local distanceY = -0.5
local animTrack = nil
local owner = nil

prompt.Triggered:Connect(function(player)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")

	if not animTrack then
		animTrack = humanoid:LoadAnimation(anim)
	end

	if not part:FindFirstChild("WeldConstraint") then
		local weld = Instance.new("WeldConstraint", part)
		part.CanCollide = false
		prompt.ActionText = "Drop"
		part.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0, distanceY, distanceZ)
		weld.Part0 = part
		weld.Part1 = character.HumanoidRootPart
		player.PlayerScripts:WaitForChild("Spin").Enabled = false
		player.Character.Humanoid.JumpPower = 25
		game.ServerScriptService.SpeedSetter.Speed.Value = 10
		animTrack:Play()
		if owner == nil then
			owner = player.Name
		end
	elseif part:FindFirstChildWhichIsA("WeldConstraint") and owner == player.Name then
		prompt.ActionText = "Pick Up"
		animTrack:Stop()
		part:FindFirstChildWhichIsA("WeldConstraint"):Destroy()
		player.PlayerScripts:WaitForChild("Spin").Enabled = true
		player.Character.Humanoid.JumpPower = 50
		game.ServerScriptService.SpeedSetter.Speed.Value = 20
		part.CanCollide = true
		owner = nil
		animTrack = nil
	end
end)

the error is line 26 (the place where it waits for the child spin) and ive tried a few different things such as no waitforchild, and player.playerscripts. the error is " PlayerScripts is not a valid member of Player “Players.rickrolledlololXD2"” or something similar when i dont use playerscripts

If that’s the case, the issue might be that the game takes some time to load to create all the stuff so maybe on top of the script add this;

repeat task.wait() until game:Isloaded

usually it takes some time to get to the part that has the proximity prompt so i dont think that’s the case

Where is the script located within the game?

inside a proximity prompt, which is inside a part

The problem is right here

player.PlayerScripts:WaitForChild("Spin").Enabled = false

Im assuming you’re trying to access StarterPlayerScript, which isn’t possible unless you’re using a Local Script, or, if you want it to be a Regular Script, use of a RemoteEvent.

Here’s a tutorial on RemoteEvents

If you don’t want to learn how to use RemoteEvents (really suggest you should use a remote event), use a Local Script. I would go into detail on how to convert your code into a Local Script but I don’t have your layout of the game. Also a lot of problems would surge up in the future if you were to use Local Script, so again, I suggest you use a RemoteEvent.

1 Like

try this:

local prompt = script.Parent
local anim = script.Carry
local anim2 = script.PickUp 

local distanceZ = -2.7
local distanceY = -0.5
local animTrack = nil
local owner = nil

prompt.Triggered:Connect(function(player)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")

	if not animTrack then
		animTrack = humanoid:LoadAnimation(anim)
	end

	if not part:FindFirstChild("WeldConstraint") then
		local weld = Instance.new("WeldConstraint", part)
		part.CanCollide = false
		prompt.ActionText = "Drop"
		part.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0, distanceY, distanceZ)
		weld.Part0 = part
		weld.Part1 = character.HumanoidRootPart
		player.PlayerScripts:WaitForChild("Spin").Disabled = true
		player.Character.Humanoid.JumpPower = 25
		game.ServerScriptService.SpeedSetter.Speed.Value = 10
		animTrack:Play()
		if owner == nil then
			owner = player.Name
		end
	elseif part:FindFirstChildWhichIsA("WeldConstraint") and owner == player.Name then
		prompt.ActionText = "Pick Up"
		animTrack:Stop()
		part:FindFirstChildWhichIsA("WeldConstraint"):Destroy()
		player.PlayerScripts:WaitForChild("Spin").Disabled = false
		player.Character.Humanoid.JumpPower = 50
		game.ServerScriptService.SpeedSetter.Speed.Value = 20
		part.CanCollide = true
		owner = nil
		animTrack = nil
	end
end)

after using remote events, i get no errors, however the events never fire. can you explain why they don’t?

local part = script.Parent.Parent
local prompt = script.Parent
local anim = script.Carry
local anim2 = script.PickUp 

local distanceZ = -2.7
local distanceY = -0.5
local animTrack = nil
local owner = nil

prompt.Triggered:Connect(function(player)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")

	if not animTrack then
		animTrack = humanoid:LoadAnimation(anim)
	end

	if not part:FindFirstChild("WeldConstraint") then
		local weld = Instance.new("WeldConstraint", part)
		part.CanCollide = false
		prompt.ActionText = "Drop"
		part.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0, distanceY, distanceZ)
		weld.Part0 = part
		weld.Part1 = character.HumanoidRootPart
		wait()
		game:GetService("ReplicatedStorage").DisableSpin:FireAllClients("DisableSpin")
		player.Character.Humanoid.JumpPower = 25
		game.ServerScriptService.SpeedSetter.Speed.Value = 10
		animTrack:Play()
		if owner == nil then
			owner = player.Name
		end
	elseif part:FindFirstChildWhichIsA("WeldConstraint") and owner == player.Name then
		prompt.ActionText = "Pick Up"
		animTrack:Stop()
		part:FindFirstChildWhichIsA("WeldConstraint"):Destroy()
		wait()
		game:GetService("ReplicatedStorage").EnableSpin:FireAllClients("EnableSpin")
		player.Character.Humanoid.JumpPower = 40
		game.ServerScriptService.SpeedSetter.Speed.Value = 20
		part.CanCollide = true
		owner = nil
		animTrack = nil
	end
end)

It may be because you are using remoteEvent:FireAllClients() rather than remoteEvent:FireClient().

The difference between these 2 is that FireAllClients, as it says, fires to all players. And FireClient only fires to one player

To use remoteEvent:FireClient() you need the player argument and optional arguments.
Example Code:

aRemoteEvent:FireClient(player, optionalArgs)

And in this case to fix (one line of) your script it should be

game:GetService("ReplicatedStorage").DisableSpin:FireClient(player)

Assuming you were trying to reference the DisableSpin remoteEvent, you don’t need the "DisableSpin" in the above code as you are already referencing it when doing game:GetService("ReplicatedStorage").DisableSpin.

Also clean up your code, one of the main rules in all of coding is Never Repeat Yourself. You can easily do this by making variables and using them.

Example: your humanoid variable can be used to rather than doing player.Character.Humanoid

So do this

humanoid.JumpPower = 25

rather than this

player.Character.Humanoid.JumpPower = 25

The below code should fix your problems and also cleans up your code

local part = script.Parent.Parent
local prompt = script.Parent
local anim = script.Carry
local anim2 = script.PickUp 

local disableSpinEvent = game:GetService("ReplicatedStorage").DisableSpin
local enableSpinEvent = game:GetService("ReplicatedStorage").EnableSpin
local SpeedScript = game.ServerScriptService.SpeedSetter

local distanceZ = -2.7
local distanceY = -0.5
local animTrack = nil
local owner = nil

prompt.Triggered:Connect(function(player)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")

	if not animTrack then
		animTrack = humanoid:LoadAnimation(anim)
	end

	if not part:FindFirstChild("WeldConstraint") then
		local weld = Instance.new("WeldConstraint", part)
		part.CanCollide = false
		prompt.ActionText = "Drop"
		part.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0, distanceY, distanceZ)
		weld.Part0 = part
		weld.Part1 = character.HumanoidRootPart
		wait()
		disableSpinEvent:FireClient(player)
		humanoid.JumpPower = 25
		SpeedScript.Speed.Value = 10
		animTrack:Play()
		if owner == nil then
			owner = player.Name
		end
	elseif part:FindFirstChildWhichIsA("WeldConstraint") and owner == player.Name then
		prompt.ActionText = "Pick Up"
		animTrack:Stop()
		part:FindFirstChildWhichIsA("WeldConstraint"):Destroy()
		wait()
		enableSpinEvent:FireClient(player)
		humanoid.JumpPower = 40
		SpeedScript.Speed.Value = 20
		part.CanCollide = true
		owner = nil
		animTrack = nil
	end
end)

the script is fine, but the script to disable spin (after receiving the signal from remote events) doesn’t work. I think it’s because it just doesn’t receive the signal. heres the script so you can see if i did something wrong

local replicatedStorage = game:GetService("ReplicatedStorage")
local spin = game:GetService("StarterPlayer").StarterPlayerScripts:WaitForChild("Spin")

local function enable()
	print("enabled")
	spin.Enabled = true
end

local function disable()
	print("disabled")
	spin.Enabled = false
end

replicatedStorage.DisableSpin.OnServerEvent:Connect(disable)
replicatedStorage.EnableSpin.OnServerEvent:Connect(enable)

It seems that you are using the wrong type of script, the above script you just gave me is a Server Script (regular script). It’s suppose to be a Local Script, and most Local Scripts containing RemoteEvents functions are usually stored in StarterPlayerScripts.

So change the above script to a Local script. Once it’s a local script you can access the Spin script within the player’s StarterPlayerScripts by doing

local player = game.Players.LocalPlayer
local spinScript = player.PlayerScripts:WaitForChild("Spin")

And to fix your functions you do

replicatedStorage.DisableSpin.OnClientEvent:Connect(function()
	spinScript.Enabled = false
end)

So to fix your script from above you first gotta change it into a Local Script and place that Local Script inside of StarterPlayerScripts. Which is accessed by
StarterPlayer → StarterPlayerScripts

And then paste the below code into that Local Script

local player = game.Players.LocalPlayer
local spinScript = player.PlayerScripts:WaitForChild("Spin")
local repStorage = game:GetService("ReplicatedStorage")

repStorage.DisableSpin.OnClientEvent:Connect(function()
	print("disabled")
	spinScript.Enabled = false
end)

repStorage.EnableSpin.OnClientEvent:Connect(function()
	print("enabled")
	spinScript.Enabled = true
end)

Note, I removed your function disable() beecause they aren’t really necessary, however if you want to use function disable() then you would do

repStorage.EnableSpin.OnClientEvent:Connect(function()
	disable() --the brackets() are necessary when calling the function
end)
1 Like

Thank you for your help, have a good day!

2 Likes