Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Script does only work in play solo mode. Why? :(

Ok, so here's the code:

repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character
local player1 = game.Players.LocalPlayer

wait(2)

local cash = player1:FindFirstChild('leaderstats').Cash
local selectedPart = player1:FindFirstChild('leaderstats').SelectedPart

function onClicked(playerWhoClicked)
	
	if (selectedPart.Value == 'Bad Conveyor') then
			
		cash.Value = cash.Value - 450
		local clone = game.ServerStorage.BadConveyor:Clone()
		clone.Parent = game.Workspace
		clone.Position = script.Parent.Position
	elseif (selectedPart.Value == 'Slow Conveyor') then
		
		cash.Value = cash.Value - 750
		local clone1 = game.ServerStorage.SlowConveyor:Clone()
		clone1.Parent = game.Workspace
		clone1.Position = script.Parent.Position
		
	elseif (selectedPart.Value == 'Normal Conveyor') then
		
		cash.Value = cash.Value - 1250
		local clone2 = game.ServerStorage.NormalConveyor:Clone()
		clone2.Parent = game.Workspace
		clone2.Position = script.Parent.Position

	elseif (selectedPart.Value == 'Fast Conveyor') then
		
		cash.Value = cash.Value - 1750
		local clone3 = game.ServerStorage.FastConveyor:Clone()
		clone3.Parent = game.Workspace
		clone3.Position = script.Parent.Position
		
	elseif (selectedPart.Value == 'Super Fast Conveyor') then
		
		cash.Value = cash.Value - 2100
		local clone4 = game.ServerStorage.SuperFastConveyor:Clone()
		clone4.Parent = game.Workspace
		clone4.Position = script.Parent.Position
		
	else
		print("ERROR")
	end
end
 
script.Parent.ClickDetector.MouseClick:connect(onClicked)

I have a code that sets SelectedPart to 'Bad Conveyor', 'Slow Conveyor', 'Normal Conveyor', 'Fast Conveyor' or 'Super Fast Conveyor' so that isn't the problem.

It doesn't say any errors. It just won't work. Pls help. I'm a newbie to roblox lua.

1 answer

ServerStorage can't be used within Local Scripts, try changing the location of the items from ServerStorage to ReplicatedStorage.

http://wiki.roblox.com/index.php?title=API:Class/ServerStorage

Localplayer can't also be used in Server Scripts, you can easily get the player using the ClickDetector's function.

http://wiki.roblox.com/index.php?title=API:Class/ClickDetector

script.Parent.ClickDetector.MouseClick:connect(function(Player)
	local cash = Player:FindFirstChild('leaderstats').Cash
	local selectedPart = Player:FindFirstChild('leaderstats').SelectedPart
	if (selectedPart.Value == 'Bad Conveyor') then
		cash.Value = cash.Value - 450
		local clone = game.ReplicatedStorage.BadConveyor:Clone()
		clone.Parent = game.Workspace
		clone.Position = script.Parent.Position
	elseif (selectedPart.Value == 'Slow Conveyor') then
		cash.Value = cash.Value - 750
		local clone1 = game.ReplicatedStorage.SlowConveyor:Clone()
		clone1.Parent = game.Workspace
		clone1.Position = script.Parent.Position
	elseif (selectedPart.Value == 'Normal Conveyor') then
		cash.Value = cash.Value - 1250
		local clone2 = game.ReplicatedStorage.NormalConveyor:Clone()
		clone2.Parent = game.Workspace
		clone2.Position = script.Parent.Position
	elseif (selectedPart.Value == 'Fast Conveyor') then
		cash.Value = cash.Value - 1750
		local clone3 = game.ReplicatedStorage.FastConveyor:Clone()
		clone3.Parent = game.Workspace
		clone3.Position = script.Parent.Position
	elseif (selectedPart.Value == 'Super Fast Conveyor') then
		cash.Value = cash.Value - 2100
		local clone4 = game.ReplicatedStorage.SuperFastConveyor:Clone()
		clone4.Parent = game.Workspace
		clone4.Position = script.Parent.Position
	else
		print("ERROR")
	end
end)

7 comments

0

It is not a local script and there not just thye cloning that doesn't work. It's everything. But I can try it anyways. Testing Disn't work. Tnx anyways

0

You have "local player1 = game.Players.LocalPlayer" at the top of your script, you can only use that in a local script.

0

But if I put the code in a local script i don't work even in solo mode

0

The thing is that it work perfectly well in solo mode but not at all when the game is published

0

The thing is, you can't access LocalPlayers in server scripts. You made one of the most common mistakes. https://scriptinghelpers.org/blog/common-mistakes

0

Ok, sorry, but how do I fix it?

0

Try that.

This discussion is preserved as it was. New answers, comments, and votes are closed.