Script not working after cloning a model to client's workspace

Good day,
I’m having difficulties getting this script I have in a model to work after cloning it from ReplicatedStorage to the client’s workspace. In specific, it won’t detect when the player touches the part. The script itself works perfectly fine when it’s normally in the workspace, for whatever reason though, it doesn’t when I clone the model it’s in to the client’s.

Here’s the code (it is in a regular script):

local pumpkinPart = script.Parent.Parent
local amount = 1
local toggle = false

script.Parent.Touched:Connect(function(hit)
	print("Start...")
	if hit.Parent:FindFirstChild("Humanoid") ~= nil and toggle == false then
		toggle = true
		print("Pumpkin Touched..")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local leaderboard = player:FindFirstChild("Pumpkin Leaderboard")
		local pumpkins = leaderboard.Pumpkins
		
		pumpkins.Value = pumpkins.Value + amount
		print("Added to total..")
		wait(0.5)
		pumpkinPart:Destroy()
	end
end)

Where the script is within the model:
image

I have already tried disabling the script in studio and then enabling it once cloned into the client’s workspace, nothing changed. I don’t get any print statements or errors in the output from this whatsoever, so I have no clue as to why this is happening.

2 Likes

Is the client cloning the pumpkin into workspace or is the server doing that?

1 Like

If the client is cloning it and not the server that is your issue.

4 Likes

try changing the script to a local script

1 Like

I’ve got a localScript in StarterPlayerScripts doing the cloning, so would that be the issue?

1 Like

Yes, you must do it in a server script.

1 Like

You can either:

A. Make everything server sided.

or

B. Create the touch events on the client and have them fire a remote to update the leaderboard.

Edit: I don’t recommend trusting the client with anything tho

1 Like

When the client clones a script from replicated storage, the instance will be copied but the source for the script itself wont be which explains why the script isn’t working.

If you change the script to a local script it will work but definitely not the best approach.

1 Like

So option B would be my best bet?

1 Like

It really depends how you want it to work. Do you want it when someone picks up a pumpkin, its removed for everyone or do you want it when someone picks up a pumpkin, than its only removed for that player (but other players can still see and pickup the pumpkin)?

1 Like

The latter, it’s meant to be a scavenger hunt. So it’d have to be one person can pick it up and others can still see it, while that player could no longer see it.

1 Like

Actually, what you could do is have everything managed by the server (which is recommended), but when a player touches the pumpkin, the server fires a remote event to that player. The players client can then delete/make the pumpkin invisible on his or her client (but it still exist on the server).

3 Likes

Alright, I suppose I can give it a try. I’m not very good when it comes to events, so do you have any suggestions on where to start exactly?

1 Like

Well, first you would need to create a remoteEvent. You can leave it in replicateStorage and call it whatever.

For this example, I’m going to create a RemoteEvent called “PumpkinEvent” which will be in ReplicatedStorage.

Server Code:

-- This is basically your previous script to detect a part being hit but this time we fire a remote after it gets hit
-- Services --
local PS = game:GetService("Players");
local RS = game:GetService("ReplicatedStorage");

-- Variables --
local PumpkinRemoteEvent = RS:WaitForChild("PumpkinEvent"); -- Wait for the remote event in replicateStorage called PumpkinEvent
local MyPumpkin = script.Parent;

-- Touch Event --
MyPumpkin.Touched:Connect(function(Hit) -- Make sure you add a debounce and basically do what you did in your old script
	local Player = PS:GetPlayerFromCharacter(Hit.Parent);
	if Player then -- Make sure that its a player
		print("Pumpkin Touched by "..Player.Name);
		PumpkinRemoteEvent:FireClient(Player, MyPumpkin); -- Use "FireClient" to fire a specific players client. We are also sending MyPumpkin as an argument		
		-- Do stuff here		
	end;
end);

Now after we fire the players client. We will need a local script to listen for whenever the PumpkinEvent is fired. So your local script would look something like the following.

Local script:

-- Services --
local RS = game:GetService("ReplicatedStorage");

-- Variables --
local PumpkinRemoteEvent = RS:WaitForChild("PumpkinEvent"); -- Wait for the remote event in replicatedStorage call PumpkinEvent

PumpkinRemoteEvent.OnClientEvent:Connect(function(ThePumpkinToRemove) -- Listen for when the server fires the players client
	if ThePumpkinToRemove and ThePumpkinToRemove:IsA("BasePart") then -- Check if the previous argument we sent exist and is a part
		ThePumpkinToRemove:Destroy(); -- Destroy the part on the client (but everyone else will still see it on the server)
	end;
end);

Thats pretty much all there is to it!! I highly recommend you learn about remote events and remote functions as they are very useful!!

Anyway, hope this helps and goodluck!

1 Like

Just incase your confused on where everything should go.

Refer to below:

1 Like

I’ve learned some about events once before, for some reason I just didn’t really get the hang of it. Either way though, thank you for your help! I’ll follow your guidelines and see what happens, if it all goes well I’ll be sure to mark this as the solution.

1 Like

Glad to hear that and goodluck! :+1:

2 Likes

Hey Ze_tsu, it’s working however I noticed something odd. The player is able to interact with a pumpkin even if they’ve collected it and is destroyed on their end. Would this be because it’s still in the studio workspace? If so, how could I overcome this issue?

1 Like

Can you screenshot what you mean?

1 Like

I took a video clip of it instead.

1 Like