Basic spawning script

Alright so, working on a spawning feature for a quick little game, been trying it out for a while but had a few issues and got it as far as I can by myself.
I’m pretty sure its just a basic issue from using GetPlayers()

Anyway, the intended result I’m trying to get is to have a box stored in serverstorage to spawn in-front of the player once the command “/spawn box” was used in chat.

Issue is, that the box does spawn, however in front of the most recently joined player, and not the speaker.

Been at this for awhile now and its becoming rather tiring to keep finding incorrect solutions.
Any help would be great to learn from and use, thank you!

Script:

function onChatted(msg, recipient, player) 

local source = string.lower(player.Name) -- Converts name of speaker to lowercase
msg = string.lower(msg)

	if (msg == "/spawn box") then --Chat Command
		local box = game.ServerStorage.StorageBox
				local boxclone = box:Clone()
		
		for _,player in ipairs(game.Players:GetPlayers()) do
			local character = player.Character
		boxclone.Parent = workspace
			boxclone.Position = character.Head.Position + Vector3.new(0,0,-5)
			end
	end 

end 

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 
game.Players.ChildAdded:connect(onPlayerEntered)
1 Like

Why are you redefining player when it is already defined in the chatted function? Also, you can use game.Players.PlayerAdded:Connect(... instead of ChildAdded.

2 Likes

Your problem is the loop. Because youre using the loop, every players characters head position is beeing used. Now the problem is since you have one clone, its beeing set to every players position… And the last player in the loop is the player that joined last.
Now to fix this, in the function youre using a player as of i can tell. Just get the players position instead of using a loop.

1 Like

Thank you so much!
I knew it was something basic but I kinda got lost after trying to fix multiple issues to a point where I couldn’t keep track of my own code.
Anyway once again thank you!! (:

1 Like