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

Ways of getting LocalPlayer in a cloned Server-Side Script?

So I need ways of getting LocalPlayer in a cloned Server-Side Script. I originally intended on using

game.Players.PlayerAdded:connect(function(player)

but because it's cloned, PlayerAdded doesnt seem to function correctly. Any other ways of getting the LocalPlayer on a server-side script?

For context, and if it helps:

game.Players.PlayerAdded:connect(function(player)
script.Parent.Touched:connect(function(hit)
	if hit.Parent.Name == "Pickaxe" then
		player.Backpack.Copper.Value = player.Backpack.Copper.Value + 1
		script.Parent:Destroy()
	end
end)
end)

Any help is much appreciated as always.

3 comments

0

ServerScripts aren't associated to a Client (player) in any way as they work on the Server. Can you tell me a bit about why you're cloning a ServerScript and I might be able to point you in the right direction

0

So it's a mining game, and there's a randomizer picker script that clones a certain ore if it is chosen. The above script is in all the ores.

0

You can't. The script doesn't know what player you want. It does not read your mind.

1 answer

given the comments on your post you'd probably be better off accessing the player via the character

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "Pickaxe" then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent.Parent)
		if plr then
			plr.Backpack.Copper.Value = player.Backpack.Copper.Value + 1
			script.Parent:Destroy()
		end
	end
end)

there's about a million things wrong with the framework of this that I can't be bothered to go into but learning things one thing at a time is probably better anyway aha.

3 comments

0

Might also be worth putting a debounce on this as Destroy() might not work immediately

0

I've got a debounce set up and it's still taking over 5 hits from the pickaxe to destroy it on occasion, could I have help with this?

0

Put the debounce with the if hit.Parent.Name == "Pickaxe" bit. Add me on Discord if you've got further questions. Instance.new("Life", Noob)#3135 is my Discord handle.

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